language-bench

Prime sieve

Sieve of Eratosthenes up to ten million.

cpu-boundmemory-bound

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 primes

Reveals: 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 / buildTime (× vs best)
Go 1.27 · go builddefault20.4 ms (1.01×)
Go 1.27 · GOAMD64=v3 go build21.2 ms (1.06×)
C 16 · gcc -O3 -march=nativedefault21.4 ms (1.07×)
C 16 · gcc -O221.4 ms (1.07×)
C 16 · gcc -O321.6 ms (1.08×)
Python 3.15 · python3default395 ms (19.7×)

Click a language to see the source code, build flags, every session and how the number was computed.