Created
February 1, 2012 20:11
-
-
Save wjlafrance/1719047 to your computer and use it in GitHub Desktop.
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 "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