Created
July 17, 2013 15:27
-
-
Save kurain/6021648 to your computer and use it in GitHub Desktop.
Comper ruby's log and math.h log
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
require 'inline' | |
class Test | |
include Math | |
inline do |builder| | |
builder.include('<math.h>') | |
builder.c <<-EOF | |
double | |
log_c(int i) | |
{ | |
return log(i); | |
} | |
EOF | |
end | |
end | |
Benchmark.bmbm do |x| | |
t = Test.new | |
rands = [] | |
10_000_000.times do | |
rands << rand(1000) | |
end | |
x.report('ruby') do | |
rands.each do |int| | |
Math.log(int) | |
end | |
end | |
x.report('inline') do | |
rands.each do |int| | |
t.log_c(int) | |
end | |
end | |
end | |
# Rehearsal ------------------------------------------ | |
# ruby 9.840000 0.000000 9.840000 ( 9.842299) | |
# inline 2.650000 0.010000 2.660000 ( 2.667835) | |
# -------------------------------- total: 12.500000sec | |
# user system total real | |
# ruby 10.410000 0.010000 10.420000 ( 10.437814) | |
# inline 2.420000 0.000000 2.420000 ( 2.422468) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment