### Kill the stupid prime computation benchmark

A recent rubyinside post pointed out how cool ruby inline was because it could give a 10x speedup. The sad thing is that this example is a terrible one, and illustrates a different point far better.

The question: How fast can one print information about the first 10,000 primes in various programming languages. In fairness, the author of the RubyInline version noted that he was forbidden to change the algorithm, but dear god, why use the stupidest thing possbile as your example? Think of the children!

Original algorithm: for every number, see if it's divisible by every number smaller than it.

**Original runtime:** 2.9 seconds (his machine; 3.45 mine).

So he rewrote it in C as an inline function in Ruby. The code is 3x longer and relies on a complicated external library and gcc, etc., in order to compile.

**Inline C runtime:** 0.3 seconds.

But: GUGH. Compute the primes in pure ruby using the Sieve of Erastothenes (you know, the one we teach students their first day?):

def prime_sieve p = Array.new(N + 1) { 1 } (2..N).each { |n| if (p[n] == 1) nx = n + n while (nx <= N) p[nx] = n if p[nx] == 1 nx += n end end } (2..N).each { |n| if p[n] == 1 puts "#{n} is a prime number" else puts "#{n} equals #{p[n]} * #{n/p[n]}" end } end

**Pure ruby, sieve runtime:** 0.1 seconds.

So, it's 3x faster than a stupid C version, shorter, has no external dependencies... please, oh script hackers of the world, please abandon the "stupid prime computation" benchmark. Okay, end rant.

(Anonymous)I think you missed the point.(I feel like noting that the primes thing has the same problem as most examples.. any example small enough to be quite clear tends to be oversimplified in such a way that it could be done differently. It is the rare feature/language/library that can have its usefulness made apparent in a bite-sized chunk of code.)

dgaRe: I think you missed the point.I didn't miss the point at all: I understand that he was looking for a simple example, and stated so, but this one is particularly egregious. The speedup is enormous and the code to do it is still simple. I'd be much more a fan of

The problem with using examples like finding primes the wrong way is that there's an implicit message that rewriting your code in C is actually the right way to do it instead of going the simpler route. At least the real microbenchmarks represent situations that you might actually need to implement or represent speedups that you might take away and put in another context.

(Anonymous)NiceAm I being pedantic by pointing out that 1 isn't a prime number?

dgaRe: Nice