Skip to content

Instantly share code, notes, and snippets.

@kurain
Created July 17, 2013 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurain/6021648 to your computer and use it in GitHub Desktop.
Save kurain/6021648 to your computer and use it in GitHub Desktop.
Comper ruby's log and math.h log
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