Skip to content

Instantly share code, notes, and snippets.

@wowdyaln
Last active June 1, 2017 13:51
Show Gist options
  • Save wowdyaln/56af2138e4824dd6b929594268c5ba75 to your computer and use it in GitHub Desktop.
Save wowdyaln/56af2138e4824dd6b929594268c5ba75 to your computer and use it in GitHub Desktop.
how much time does Ruby spend in executing a function
def howmanyPrime(number)
dd = 3
dr = 2
output = []
for dd in 3..number
for dr in 2..(number-1)
if dd%dr == 0
break
elsif dr+1 == dd
output.push(dd)
next
end
end
end
return output
end
# 測時間_1
a = Time.now
p howmanyPrime(100000)
b = Time.now
puts "#{(b-a)*1000} mils"
# 測時間_2
require 'benchmark'
Benchmark.bm do |x|
x.report do
p howmanyPrime(100000)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment