Subject: More: C++ vs. Python vs. Perl vs. PHP performance benchmark |
From: Isaac Gouy |
Date: 19.6.2012 г. 22:44 ч. |
To: Ivan Zahariev |
4) Your blog post says -- "The benchmarks here do not try to be complete,
as they are showing the performance of the languages in one aspect, and mainly: loops, arrays with numbers, basic math operations." -- but what is really being shown for your Java program is unnecessary Integer boxing/unboxing.
Actually, that's easy to demonstrate, just change int[] to Integer[] (see attached program) /* int[] real 0m3.156s user 0m4.388s sys 0m0.204s */ /* Integer[] real 0m9.898s user 0m24.106s sys 0m0.444s */ best wishes, Isaac
/* int[] real 0m3.156s user 0m4.388s sys 0m0.204s */ /* Integer[] real 0m9.898s user 0m24.106s sys 0m0.444s */ import java.util.ArrayList; public final class SimplePrimesInteger { static ArrayList<Integer> primes7(int n){ ArrayList<Integer> res = new ArrayList<Integer>(); if (n < 2) { return res; } res.add(2); if (n == 2) { return res; } // do only odd numbers starting at 3 Integer[] s = n%2==0 ? new Integer[n/2 - 1] : new Integer[n/2]; for (int i=3, j=0; i <= n; i+=2, j++) s[j] = i; int mroot = (int)Math.sqrt(n); int half = s.length; int i = 0; int m = 3; while (m <= mroot){ if (s[i] != 0){ int j = (m*m-3)/2; s[j] = 0; while (j < half){ s[j] = 0; j += m; } } i++; m = 2*i+3; } for (int j : s) if (j !=0) res.add(j); return res; } public static void main(String[] args){ for (int i=0; i < 10; i++) System.out.printf("Found %d prime numbers.\n", primes7(10000000).size()); } }