Sha256: 81f3f4be9481903def67f35365b7be8afdd6823486fcab8a973baf590941ae59

Contents?: true

Size: 1.07 KB

Versions: 32

Compression:

Stored size: 1.07 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|
    prime = primes[i]
    if prime > largest
      largest = prime
    end
  end
  largest
end

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

  # Find the primes
  primes = find_primes(random_array)

  # Find the largest primes
  find_largest(primes)
end

Version data entries

32 entries across 32 versions & 2 rubygems

Version Path
ruby-prof-1.7.1-x64-mingw-ucrt test/prime.rb
ruby-prof-1.7.1 test/prime.rb
ruby-prof-1.7.0-x64-mingw-ucrt test/prime.rb
ruby-prof-1.7.0 test/prime.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/ruby-prof-1.6.3/test/prime.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/ruby-prof-1.6.3/test/prime.rb
ruby-prof-1.6.3-x64-mingw-ucrt test/prime.rb
ruby-prof-1.6.3 test/prime.rb
ruby-prof-1.6.2-x64-mingw-ucrt test/prime.rb
ruby-prof-1.6.2 test/prime.rb
ruby-prof-1.6.1 test/prime.rb
ruby-prof-1.6.1-x64-mingw-ucrt test/prime.rb
ruby-prof-1.5.0-x64-mingw-ucrt test/prime.rb
ruby-prof-1.5.0 test/prime.rb
ruby-prof-1.4.5-x64-mingw-ucrt test/prime.rb
ruby-prof-1.4.5 test/prime.rb
ruby-prof-1.4.4-x64-mingw-ucrt test/prime.rb
ruby-prof-1.4.4 test/prime.rb
ruby-prof-1.4.3-x64-mingw32 test/prime.rb
ruby-prof-1.4.3 test/prime.rb