Last active
September 12, 2019 03:29
-
-
Save tomfakes/b492fa333307a08cfa502e3a6d286df7 to your computer and use it in GitHub Desktop.
Pathname.join (Rails.root.join) vs String Interpolation benchmark
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
# frozen_string_literal: true | |
require "benchmark/ips" | |
require "pathname" | |
Benchmark.ips do |x| | |
x.config(:time => 5, :warmup => 2) | |
root = Pathname.new("/home/foo/bar/app") | |
x.report("pathname join") do |times| | |
i = 0 | |
while i < times | |
var = root.join("tmp", "path", "file").to_s | |
i += 1 | |
end | |
end | |
x.report("string iterp.") do |times| | |
i = 0 | |
while i < times | |
var = "#{root}/tmp/path/file" | |
i += 1 | |
end | |
end | |
x.compare! | |
end |
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
Warming up -------------------------------------- | |
pathname join 5.643k i/100ms | |
string iterp. 252.100k i/100ms | |
Calculating ------------------------------------- | |
pathname join 57.791k (± 4.4%) i/s - 293.436k in 5.086740s | |
string iterp. 3.994M (± 2.1%) i/s - 20.168M in 5.051901s | |
Comparison: | |
string iterp.: 3993877.9 i/s | |
pathname join: 57791.3 i/s - 69.11x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment