Prime sieve
Sieve of Eratosthenes up to ten million.
What is measured
Marks the composites up to 10,000,000 in a byte array with the classic sieve (inner loop starts at p*p), then prints the number of primes, the largest prime and the sum of all primes.
Pseudocode
n = 10,000,000
is_prime = array of n+1 bytes, all 1; is_prime[0] = is_prime[1] = 0
for p = 2 while p*p <= n:
if is_prime[p]:
for m = p*p; m <= n; m += p:
is_prime[m] = 0
print count of primes, largest prime, sum of primesReveals: Strided memory writes over a ten-megabyte array: cache and memory bandwidth plus loop overhead.
Caveat: Python and Ruby use their fastest idiom for large byte arrays where one exists (slice assignment in Python; a byte string with setbyte in Ruby and PHP). Other languages use a plain byte or boolean array.
Results (newest release of each language)
| Language / build | Time (× vs best) | |
|---|---|---|
| Go 1.27 · go builddefault | 20.4 ms (1.01×) | |
| Go 1.27 · GOAMD64=v3 go build | 21.2 ms (1.06×) | |
| C 16 · gcc -O3 -march=nativedefault | 21.4 ms (1.07×) | |
| C 16 · gcc -O2 | 21.4 ms (1.07×) | |
| C 16 · gcc -O3 | 21.6 ms (1.08×) | |
| Python 3.15 · python3default | 395 ms (19.7×) |
Click a language to see the source code, build flags, every session and how the number was computed.