summitpush / performance-tests

Some code I was playing around with to benchmark different ruby interpreters

This URL has Read+Write access

performance-tests / gen.rb
100644 25 lines (21 sloc) 0.545 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
require 'benchmark'
 
def generate_primes(max_prime)
  @prime_hash = Hash.new { |h,k| h[k] = true }
  @primes = [ 2 ]
  3.step(max_prime, 2) do |next_prime|
    if(@prime_hash[next_prime])
      @primes << next_prime
      if(next_prime < Math.sqrt(max_prime))
        next_prime.step(max_prime/next_prime, 2) { |i| @prime_hash[i * next_prime] = false }
      end
    end
  end
end
 
 
max = 10**6
Benchmark.bm(40) do |bm|
  5.times do
    bm.report("Generating all primes less than #{max}") do
      generate_primes(max)
    end
  end
end