11. Pythonのソースコード
import sys
import time
before = time.time()
num_steps = 10000000
sum = 0
step = 1.0 / num_steps
for i in range(1,num_steps):
x = (i - 0.5) * step
sum = sum + 4.0 / (1.0 + x * x)
pi = step * sum
print pi
print "¥n"
after = time.time()
print "Running Time =", after - before, "s"
12. Rubyのソースコード
require "benchmark"
num_steps = 10000000
step = 1.0 / num_steps
sum = 0
puts Benchmark::CAPTION
puts Benchmark.measure{
for i in 1 .. num_steps
x = (i - 0.5) * step
sum = sum + 4.0 / (1.0 + x * x)
end
pi = step * sum
puts pi
}