Sha256: c940b397fcdc930e8474a9c2fc8bd2e2b1998fb538cf51e036aa6d761e2ba4a7

Contents?: true

Size: 1.05 KB

Versions: 17

Compression:

Stored size: 1.05 KB

Contents

# A silly little test program that finds prime numbers.  It
# is intentionally badly designed to show off the use
# of ruby-prof.
#
# Source from http://people.cs.uchicago.edu/~bomb154/154/maclabs/profilers-lab/

def make_random_array(length, maxnum)
  result = Array.new(length)
  result.each_index do |i|
    result[i] = rand(maxnum)
  end

  result
end

def is_prime(x)
  y = 2
  y.upto(x-1) do |i|
    return false if (x % i) == 0
  end
  true
end

def find_primes(arr)
  result = arr.select do |value|
    is_prime(value)
  end
  result
end

def find_largest(primes)
  largest = primes.first

  # Intentionally use upto for example purposes
  # (upto is also called from is_prime)
  0.upto(primes.length-1) do |i|
    sleep(0.02)
    prime = primes[i]
    if prime > largest
      largest = prime
    end
  end
  largest
end

def run_primes(length=10, maxnum=1000)
  # Create random numbers
  random_array = make_random_array(length, maxnum)

  # Find the primes
  primes = find_primes(random_array)

  # Find the largest primes
  # largest = find_largest(primes)
end

Version data entries

17 entries across 17 versions & 3 rubygems

Version Path
ruby-prof-0.11.0.rc3-x86-mingw32 test/prime.rb
ruby-prof-0.11.0.rc3 test/prime.rb
ruby-prof-0.11.0.rc2-x86-mingw32 test/prime.rb
ruby-prof-0.11.0.rc2 test/prime.rb
ruby-prof-0.11.0.rc1-x86-mingw32 test/prime.rb
ruby-prof-0.11.0.rc1 test/prime.rb
ruby_prof-json-0.0.1 test/prime.rb
ruby-prof-0.10.8 test/prime.rb
ruby-prof-0.10.7 test/prime.rb
ruby-prof-0.10.6 test/prime.rb
ruby-prof-0.10.5 test/prime.rb
ruby-prof-0.10.4 test/prime.rb
ruby-prof-0.10.2 test/prime.rb
acunote-ruby-prof-0.9.2 test/prime.rb
ruby-prof-0.9.2 test/prime.rb
ruby-prof-0.9.1 test/prime.rb
ruby-prof-0.9.0 test/prime.rb