Sieve of Eratosthenes

Mathieu mathk.sue at gmail.com
Mon Oct 9 21:22:36 UTC 2006


Davide Varvello a écrit :
> Hi All,
> I did some performance tests comparing java and squeak. The java code is this:
> public class GeneratePrimes {
> 	public static double generate(int n) {
> 		long start = System.currentTimeMillis();
> 		boolean sieve[] = new boolean[n];
> 		Arrays.fill(sieve, true);
> 		sieve[0] = false;
> 		sieve[1] = false;
> 		for (int i = 2; i < n; i++) {
> 			if (sieve[i]) {
> 				for (int j = 2 * i; j < n; j += i) {
> 					sieve[j] = false;
> 				}
> 			}
> 		}
> 
> 		return (System.currentTimeMillis() - start);
> 	}
> 
> The Squeak one is:
> 
> runOn: aNumber 
> 	| sieve |
> 
> 	sieve := Array new: aNumber withAll: true.
> 	sieve at: 1 put: false.
> 
> 	(2 to: aNumber) do: [:i | (sieve at: i) ifTrue: [(i to: aNumber by: i) do: [:k | sieve at: k put: false] ]
> 
> The Squeak code is better (of course) but on a number of 1000000 Java spends 94 msec instead Squeak 2650!
> Am I using the right approach or should I change something in my squeak code to make it perform better (but preserving clearness)?
> 
> Win xp, 3 ghz, Squeak 3.8, jdk 1.4.2 
> 
> Thanks
>  Davide
> 
> 
> ------------------------------------------------------------------------
> 
> 

Wy do you use Interval this slowdown a lote the test. Prefer this:

runOn: aNumber
 	| sieve |

 	sieve := Array new: aNumber withAll: true.
 	sieve at: 1 put: false.

 	2 to: aNumber do: [:i | (sieve at: i) ifTrue: [i to: aNumber by: i do: [:k | sieve at: k put: false] ]


Interval creation take a bit time for me with your first solution I have got 1444ms and with the
second only 1051ms.

	Math



More information about the Squeak-dev mailing list