Sieve of Eratosthenes

Davide Varvello d.varvello at quinary.com
Mon Oct 9 20:52:45 UTC 2006


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/ms-tnef
Size: 2918 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20061009/6dc3e1c5/attachment.bin


More information about the Squeak-dev mailing list