Skip to content

Instantly share code, notes, and snippets.

@coldnebo
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save coldnebo/64717687764bb11aa3b5 to your computer and use it in GitHub Desktop.

Select an option

Save coldnebo/64717687764bb11aa3b5 to your computer and use it in GitHub Desktop.
measure how fast gem require is.
require 'benchmark'
include Benchmark
def with_require
fork { require 'rake' }
end
def without_require
fork {}
end
# just a sanity check
def with_require_nofork
require 'rake'
end
n = 200
puts "how fast is require?"
Benchmark.benchmark(CAPTION, 7, FORMAT, "require:") do |x|
wr = x.report("with:") { for i in 1..n; with_require; end }
wor = x.report("without:") { for i in 1..n; without_require; end }
[wr-wor]
end
puts "in case you don't believe the above times, a sanity check"
Benchmark.benchmark(CAPTION, 15, FORMAT) do |x|
nf = x.report("nofork require:") { for i in 1..n; with_require_nofork; end }
end
@coldnebo

coldnebo commented Dec 6, 2014

Copy link
Copy Markdown
Author

Output:

$ ruby -v
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux]
$ ruby require_speedtest.rb 
how fast is require?
              user     system      total        real
with:     0.030000   0.020000   0.050000 (  6.135141)
without:  0.010000   0.020000   0.030000 (  1.273201)
require:  0.020000   0.000000   0.020000 (  4.861940)
in case you don't believe the above times, a sanity check
                      user     system      total        real
nofork require:   0.030000   0.000000   0.030000 (  0.042729)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment