I believe in java most people use String.split() to split a String to pieces. That method is there for ages (java 1.4), everyone knows it and it just works.
The alternative for this is to use a Pattern instance, which is immutable, therefore you only need a single instance of the pattern created only once and it can serve your applications forever. The guys who started to use it are in my opinion smarter, because they knew that the String.split actually needs to create a Pattern object, which is a relatively heavy operation and they can save it.
However, this is not the end of the story. It would be too short and would not make a blog post :-)
String.split() is smart, and it has a special case when the pattern is only one or two characters long and it does not contain special regexp characters. In this case it will not create a Pattern object, but simply process the whole thing in place. That special piece of code is not just accidentally there.
Let's see the speed comparison.
As you see, String.split performs better when the character you use for splitting meets the above requirements. When you need to split with several different characters - I believe this might be the less frequent case - you'd be much better using a Pattern constant.
Thursday, 4 July 2013
Sunday, 31 March 2013
compression again - system vs java implementation
Last time I mentioned that using the operating system's compression utility (gzip on linux) performs better than the java counterpart even if you use it from java (the good old Process.exec()). This is not quite that simple of course :-) So in this test I compare the time needed to compress a random input both by system and java implementations. The size of the input is growing over the test, so does the time needed to compress, but there is something interesting.
So as you see the system gzip is faster, but it has a communication and process creation overhead. The java implementation is running without this overhead is therefore performing better with small inputs. The lines meet at about 512 KB. If the input is bigger, piping through a system command performs better.
This test was performed on Fedora 18 (the disasterous OS) x64 architecture, other architectures and operating systems may have different result.
So as you see the system gzip is faster, but it has a communication and process creation overhead. The java implementation is running without this overhead is therefore performing better with small inputs. The lines meet at about 512 KB. If the input is bigger, piping through a system command performs better.
This test was performed on Fedora 18 (the disasterous OS) x64 architecture, other architectures and operating systems may have different result.
Monday, 18 March 2013
To kill a GC
This is actually an answer for a recent patch written at work. So what happens if you have an object, which overrides the finallize method and in that it needs to wait for a while, e.g. to wait for a thread to join (database transaction to finish, IO operation, and so on).
This is an example code, it does not wait for a thread for 20 seconds, it only sleeps for 1, but anyway it gives a little work for the GC.
Let's use the jconsole to look under the hood of the VM.
I think this is visual enough. So in general, I agree with those who try to avoid having a finallize method in their code. And really why should you have one?
This is an example code, it does not wait for a thread for 20 seconds, it only sleeps for 1, but anyway it gives a little work for the GC.
import java.util.Date; public class Main { final int nr; final byte[] justsomememory; public Main(int nr) { this.nr = nr; justsomememory = new byte[1024 * 1024]; } public static void main(String[] args) throws Exception { for (int i = 0; i < 20000; i++) { Main main = new Main(i); Thread.sleep(10); } } @Override protected void finalize() throws Throwable { Thread.sleep(1000); } }
Let's use the jconsole to look under the hood of the VM.
![]() |
This is a healthy memory usage. |
![]() |
This is what you see if you have any blocking operations in finaly() |
Monday, 4 February 2013
Caching UUID's?
This is a short test inspired by my day work. The question is: is it worth caching objects in memory that could be just parsed? Examples for such objects are Integers, UUID's and some other objects. Well, as you know the first some Integers are actually cached by the runtime, so if you call Integer.parseInt, you may already get cached instances. That makes sense with Integer.
With UUID the situation is a bit different, since there are no "first X" UUID's. So let's say that if you use some guid's frequently. The question is: can you get any advantage out of caching UUID objects rather than parsing from string?
So most importantly, let's measure just plain parsing (no cache) just to compare to something. Then, let's measure caching with a HashMap. I have to add that a HashMap is not a cache and whenever I see a HashMap used as cache, I have terrible nightmares about OOM exceptions coming like zombies from everywhere.
Third, let's measure the performace of a real cache, I chose ehcache. You can choose your own pet cache technology (to be honest I use infinispan, but now for simplicity I just wanted a good old ehcache)
So my conclusion is: I would probably not want to cache the objects that are this easy to create. I would definetly try to optimize once the database interactions are optimal, the app scales well to multiple processors and even multiple nodes, and so on... but this looks like a small and painful victory.
Test method
All data series are measured with a 10 different datasets. The datasets differ from each other in the number of repeating UUID's: The first one does not have any, the last one has 90 percent repeating UUID's.So most importantly, let's measure just plain parsing (no cache) just to compare to something. Then, let's measure caching with a HashMap. I have to add that a HashMap is not a cache and whenever I see a HashMap used as cache, I have terrible nightmares about OOM exceptions coming like zombies from everywhere.
Third, let's measure the performace of a real cache, I chose ehcache. You can choose your own pet cache technology (to be honest I use infinispan, but now for simplicity I just wanted a good old ehcache)
Results
Ok, let's see what we got.
- As expected, no cache performs more or less the same everytime.
- HashMap "caching" adds a little speed over 50 percent repeating input. It is a little compensatin for the OOM's you will get, or for the code you will write to avoid it :)
- Ehcache implementation has some difficulties keeping up, it only beats the "no cache" solution when the percentage of repeating uuid's is over 90%, even then, the gain is little.
So my conclusion is: I would probably not want to cache the objects that are this easy to create. I would definetly try to optimize once the database interactions are optimal, the app scales well to multiple processors and even multiple nodes, and so on... but this looks like a small and painful victory.
Monday, 3 December 2012
compression - size vs speed
This is a small comparison of compression algorithms for the JVM:
- gzip of course, I tested both java's implementation and the one included in Linux (through System.exec)
- bzip2, the more heavy-weight player
- deflate, the algoithm used by zip
- pk200, a seemingly java-specific algorithm
- xz, the algorithm behind 7zip
Random input
Text input
Friday, 30 November 2012
Kotlin arrays
Let's kick the CPU today with Kotlin. If you haven't heard about kotlin, it is a relatively young JVM language developed by Jetbrains, the guys behind the popular Idea IDE, Teamcity (still my favourite continuous integration tool) and some other tools.
The obvious advantage of kotlin over other alternate JVM languages is that it does have a good IDE integration. I never really liked the groovy IDE plugins (it must be harder to write good IDE for a dynamic language) and never liked scala's eclipse plugin at all :-( It is always outdated, but never really stable. So kotlin, in it early days of creation already has some advantage over the competition.
I think kotlin language itself borrowed a lot from scala, for example everything is an object (long time debate in the java language), it supports closures and there are even more immutable objects.
One potential problem I have run into is the use of Arrays. In kotlin, you either have to pass an initializer closure to the array constructor (e.g. Array<Int>(1024, {i -> i+1})) or you can choose from the java-interoperability arrays: IntArray, FloatArray, DoubleArray, ByteArray, etc. While the java-compatible arrays are just as quick as in java (since they are compiled into java), the Array objects are rather slow and heavy to create. When I ran into this problem, I wanted to know how much slower are they.
So what you see here is that an IntArray is a lot faster than an Array<Int>, however Array<String> is not much slower than Array<Int> (probably since String does not have to be boxed/unboxed). Beware of autoboxing :-)
One day, when we will have something like findbugs for alternative languages like kotlin, probably Array<Int> will have a serious warning in the performance section.
The obvious advantage of kotlin over other alternate JVM languages is that it does have a good IDE integration. I never really liked the groovy IDE plugins (it must be harder to write good IDE for a dynamic language) and never liked scala's eclipse plugin at all :-( It is always outdated, but never really stable. So kotlin, in it early days of creation already has some advantage over the competition.
I think kotlin language itself borrowed a lot from scala, for example everything is an object (long time debate in the java language), it supports closures and there are even more immutable objects.
One potential problem I have run into is the use of Arrays. In kotlin, you either have to pass an initializer closure to the array constructor (e.g. Array<Int>(1024, {i -> i+1})) or you can choose from the java-interoperability arrays: IntArray, FloatArray, DoubleArray, ByteArray, etc. While the java-compatible arrays are just as quick as in java (since they are compiled into java), the Array objects are rather slow and heavy to create. When I ran into this problem, I wanted to know how much slower are they.
So what you see here is that an IntArray is a lot faster than an Array<Int>, however Array<String> is not much slower than Array<Int> (probably since String does not have to be boxed/unboxed). Beware of autoboxing :-)
One day, when we will have something like findbugs for alternative languages like kotlin, probably Array<Int> will have a serious warning in the performance section.
Wednesday, 24 October 2012
Pubsubhubbub and the Big One
I am keeping an eye on the pubsubhubbub technology since its first day, it is a very simple, yet interesting technology to make RSS/Atom feeds really fast without polling. I wrote my own component called 'SubHub', it is running for a couple of weeks now, it is receiving tons of Atom and RSS messages from popular news-sources like nasa, cnn, bbc and the few thousand other, through pubsubhubbub. Thanks to this hard-to-spell technology, I do not poll at all :-)
The developers envisioned a distributed network of hubs and streams and sure it is distributed by design. What I wanted to know is how distributed is it, since my first guess was that Google will have a little dominance in it. For example blogger.com rss feeds (including Dummy Warhead's) are pointing to google's pubsubhubbub server. The WordPress guys were also very active and they implemented their WordPress plugin, which is turned on in all the wordpress.com blogs, but they only use it for their own content while Google's service is used by anyone... and almost everyone.
Enough talking. Let's see, who is doing most of the work in pubsubhubbub:
The graph was generated from the web server log using the usual linux commands.
So the two upcoming players are SuperFeedr and Pheedo, looks like they are special services for special customers. There might be some minor players in the technology, but I haven't found them yet.
The developers envisioned a distributed network of hubs and streams and sure it is distributed by design. What I wanted to know is how distributed is it, since my first guess was that Google will have a little dominance in it. For example blogger.com rss feeds (including Dummy Warhead's) are pointing to google's pubsubhubbub server. The WordPress guys were also very active and they implemented their WordPress plugin, which is turned on in all the wordpress.com blogs, but they only use it for their own content while Google's service is used by anyone... and almost everyone.
Enough talking. Let's see, who is doing most of the work in pubsubhubbub:
The graph was generated from the web server log using the usual linux commands.
So the two upcoming players are SuperFeedr and Pheedo, looks like they are special services for special customers. There might be some minor players in the technology, but I haven't found them yet.
Subscribe to:
Posts (Atom)