Subject: Re: C++ vs. Python vs. Perl vs. PHP performance benchmark |
From: Isaac Gouy |
Date: 20.6.2012 г. 01:43 ч. |
To: Ivan Zahariev |
Hi Ivan
Why would you think that I want to make Java look slow?
Because that's what you've done :-)
This is a fair competition with fair rules -- every language uses the very same algorithm.
I'll answer in 2 ways -- 1) The program I provided uses the same algorithm - The Sieve of Eratosthenes. What it doesn't do is force Java to be written as though it was Python. 2) Not a fair competition! Your Java program adds one element at a time to ArrayList s -- but your Python program uses a special function to create the dynamic array s. To be what you call fair, your Python program should add one element at a time to array s. Measure the runtime of the attached program slowerprimes.py
*did* require that we use dynamic arrays everywhere, to compare how each language does the job in regards to performance.The idea is, if we used another algorithm which
If you care about dynamic array performance then don't you think you should use an algorithm that adds and removes elements from a dynamic array?
Note that your optimizations also apply to C++
Yes, the *ordinary* way to write the Sieve of Eratosthenes that every C++ programmer would take is to use a fixed size array. The Sieve of Eratosthenes algorithm does not grow or shrink the array! If you insist on using Java ArrayList when it isn't needed, then at least set an initial heap size that will allow the ArrayList to grow in the way you wish. Run your Java program like this -- $ time java -Xms500m PrimeNumbersBenchmarkApp best wishes, Isaac
mport psyco #psyco.full() def get_primes7(n): """ standard optimized sieve algorithm to get a list of prime numbers --- this is the function to compare your functions against! --- """ if n < 2: return [] if n == 2: return [2] # do only odd numbers starting at 3 s = [] i = 3 while i <= n: s.append(i) i = i+2 # n**0.5 simpler than math.sqr(n) mroot = n ** 0.5 half = len(s) i = 0 m = 3 while m <= mroot: if s[i]: j = (m*m-3)//2 # int div s[j] = 0 while j < half: s[j] = 0 j += m i = i+1 m = 2*i+3 return [2]+[x for x in s if x] for t in range(10): res = get_primes7(10000000) print "Found", len(res), "prime numbers."