Skip to content

Instantly share code, notes, and snippets.

@BenMorganIO
Last active August 29, 2015 14:01
Show Gist options
  • Save BenMorganIO/94803b3197b8e5d04e14 to your computer and use it in GitHub Desktop.
Save BenMorganIO/94803b3197b8e5d04e14 to your computer and use it in GitHub Desktop.
Math Gist
require 'benchmark'
n = 100000000
Benchmark.bm do |benchmark|
benchmark.report do
n.times { 2 + 2 } # Fastest Addition
end
benchmark.report do
n.times { 2.+ 2 } # Slowest Addition
end
benchmark.report do
n.times { 2.+(2) }
end
benchmark.report do
n.times { 2 * 2 } # Fastest Multiplication
end
benchmark.report do
n.times { 2.* 2 }
end
benchmark.report do
n.times { 2.*(2) } # Slowest Multiplication
end
benchmark.report do
n.times { 2 ** 2 }
end
benchmark.report do
n.times { 2.** 2 } # Fastest Exponent
end
benchmark.report do
n.times { 2.**(2) } # Slowest Exponent
end
end
user system total real
5.930000 0.010000 5.940000 ( 5.936177)
6.190000 0.000000 6.190000 ( 6.196271)
6.010000 0.000000 6.010000 ( 6.018421)
7.770000 0.010000 7.780000 ( 7.773181)
7.770000 0.000000 7.770000 ( 7.786711)
7.790000 0.010000 7.800000 ( 7.807730)
10.380000 0.010000 10.390000 ( 10.391489)
10.370000 0.020000 10.390000 ( 10.391462)
10.580000 0.010000 10.590000 ( 10.620809)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment