Skip to content

Instantly share code, notes, and snippets.

@wjlafrance
Created February 1, 2012 20:11
Show Gist options
  • Save wjlafrance/1719047 to your computer and use it in GitHub Desktop.
Save wjlafrance/1719047 to your computer and use it in GitHub Desktop.
require "inline"
class Time
inline(:C) do |builder|
# c_singleton creates class method
builder.c_singleton "long epoch_sec() { return time(NULL); }"
end
end
require 'benchmark'
# Class method, native C - fast
puts Benchmark.measure {
1_000_000.times.each do
Time.epoch_sec
end
}
# Ruby - sexy, not fast
puts Benchmark.measure {
1_000_000.times.each do
Time.now.to_i
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment