Update: 2009-05-29
Thanks for all the suggestions and advice. I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago. In the end I was able to make the java code the fastest.
Lessons:
My example code below shows the insertion of primitive ints but the production code is actually storing strings (my bad). When I corrected that the python execution time went from 2.8 seconds to 9.6. So right off the bat, the java was actually faster when storing objects.
But it doesn't stop there. I had been executing the java program as follows:
java -Xmx1024m SpeedTest
But if you set the initial heap size as follows you get a huge improvement:
java -Xms1024m -Xmx1024m SpeedTest
This simple change reduced the execution time by more than 50%. So the final result for my SpeedTest is python 9.6 seconds. Java 6.5 seconds.
Original Question:
I had the following python code:
import time
import sys
def main(args):
iterations = 10000000
counts = set()
startTime = time.time();
for i in range(0, iterations):
counts.add(i)
totalTime = time.time() - startTime
print 'total time =',totalTime
print len(counts)
if __name__ == "__main__":
main(sys.argv)
And it executed in about 3.3 seconds on my machine but I wanted to make it faster so I decided to program it in java. I assumed that because java is compiled and is generally considered to be faster than python I would see some big paybacks.
Here is the java code:
import java.util.*;
class SpeedTest
{
public static void main(String[] args)
{
long startTime;
long totalTime;
int iterations = 10000000;
HashSet counts = new HashSet((2*iterations), 0.75f);
startTime = System.currentTimeMillis();
for(int i=0; i<iterations; i++)
{
counts.add(i);
}
totalTime = System.currentTimeMillis() - startTime;
System.out.println("TOTAL TIME = "+( totalTime/1000f) );
System.out.println(counts.size());
}
}
So this java code does basically the same thing as the python code. But it executed in 8.3 seconds instead of 3.3.
I have extracted this simple example from a real-world example to simplify things. The critical element is that I have (set or hashSet) that ends up with a lot of members much like the example.
Here are my questions:
How come my python implementation is faster than my java implementation?
Is there a better data structure to use than the hashSet (java) to hold a unique collection?
What would make the python implementation faster?
What would make the java implementation faster?
UPDATE:
Thanks to all who have contributed so far. Please allow me to add some details.
I have not included my production code because it is quite complex. And would generate a lot of distraction. The case I present above is the most simplified possible. By that I mean that the java put call seems to be much slower than the python set`s add().
The java implementation of the production code is also about 2.5 - 3 times slower than the python version -- just like the above.
I am not concerned about vm warmup or startup overhead. I just want to compare the code from my startTime to my totalTime. Please do not concern yourselves with other matters.
I initialized the hashset with more than enough buckets so that it should never have to rehash. (I will always know ahead of time how many elements the collection will ultimately contain.) I suppose one could argue that I should have initialized it to iterations/0.75. But if you try it you will see that execution time is not significantly impacted.
I set Xmx1024m for those that were curious (my machine has 4GB of ram).
I am using java version: Java(TM) SE Runtime Environment (build 1.6.0_13-b03).
In the production version of I am storing a string (2-15 chars) in the hashSet so I cannot use primitives, although that is an interesting case.
I have run the code many, many times. I have very high confidence that the python code is between 2.5 and 3 times faster than the java code.
There's a number of issues here which I'd like to bring together.
First if it's a program that you are only going to run once, does it matter it takes an extra few seconds?
Secondly, this is just one microbenchmarks. Microbenchmarks are pointless for comparing performance.
Startup has a number of issues.
The Java runtime is much bigger than Python so takes longer to load from disk and takes up more memory which may be important if you are swapping.
If you haven't set
-Xms
you may be running the GC only to resize the heap. Might as well have the heap properly sized at the start.It is true that Java starts off interpreting and then compiles. Around 1,500 iterations for Sun client [C1] Hotspot and 10,000 for server [C2]. Server Hotspot will give you better performance eventually, but take more memory. We may see client Hotspot use server for very frequently executed code for best of both worlds. However, this should not usually be a question of seconds.
Most importantly you may be creating two objects per iteration. For most code, you wouldn't be creating these tiny objects for such a proportion of the execution. TreeSet may be better on number of objects score, with 6u14 and Harmony getting even better.
Python may possibly be winning by storing small integer objects in references instead of actually have an object. That is undoubtably a good optimisation.
A problem with a lot of benchmarks is you are mixing a lot of different code up in one method. You wouldn't write code you cared about like that, would you? So why are you attempting to performance test which is unlike code you would actually like to run fast?
Better data structure: Something like
BitSet
would seem to make sense (although that has ynchronisation on it, which may or may not impact performance).Well, if you're going to tune the Java program, you might as well tune the Python program too.
Just using
xrange
makes it about 8% faster on my machine. And the expressionset(xrange(10000000))
builds exactly the same set, but 2.5x faster (from 1.87 seconds to 0.74).I like how tuning a Python program makes it shorter. :) But Java can do the same trick. As everyone knows, if you want a dense set of smallish integers in Java, you don't use a hash table. You use a
java.util.BitSet
!That should be fairly quick. Unfortunately I don't have the time to test it right now.
I'd like to dispel a couple myths I saw in the answers:
Java is compiled, yes, to bytecode but ultimately to native code in most runtime environments. People who say C is inherently faster aren't telling the entire story, I could make a case that byte compiled languages are inherently faster because the JIT compiler can make machine-specific optimizations that are unavailable to way-ahead-of-time compilers.
A number of things that could make the differences are:
You're not really testing Java vs. Python, you're testing
java.util.HashSet
using autoboxed Integers vs. Python's native set and integer handling.Apparently, the Python side in this particular microbenchmark is indeed faster.
I tried replacing HashSet with
TIntHashSet
from GNU trove and achieved a speedup factor between 3 and 4, bringing Java slightly ahead of Python.The real question is whether your example code is really as representative of your application code as you think. Have you run a profiler and determined that most of the CPU time is spent in putting a huge number of ints into a HashSet? If not, the example is irrelevant. Even if the only difference is that your production code stores other objects than ints, their creation and the computation of their hashcode could easily dominate the set insertion (and totally destroy Python's advantage in handling ints specially), making this whole question pointless.
You might want to see if you can "prime" the JIT compiler into compiling the section of code you're interested in, by perhaps running it as a function once beforehand and sleeping briefly afterwords. This might allow the JVM to compile the function down to native code.
Are you using the -server flag with the jvm? You can't test for performance without it. (You also have to warm up the jvm before doing the test.)
Also, you probably want to use
TreeSet<Integer>
. HashSet will be slower in the long run.And which jvm are you using? The newest I hope.
EDIT
When I say use TreeSet, I mean in general, not for this benchmark. TreeSet handles the real world issue of non even hashing of objects. If you get too many objects in the same bin in a HashSet, you will perform about O(n).