Skip to content

Instantly share code, notes, and snippets.

@madwork
Created March 12, 2013 18:29
Show Gist options
  • Save madwork/5145526 to your computer and use it in GitHub Desktop.
Save madwork/5145526 to your computer and use it in GitHub Desktop.
String prepend FTW
require 'benchmark'
require 'benchmark/ips'
Benchmark.ips do |x|
x.report("String::prepend") { "foo".prepend "tmp/" }
x.report("File.join") { File.join "tmp", "foo" }
end
puts
Benchmark.bm do |x|
x.report("String::prepend") { 1000000.times{"foo".prepend "tmp/"} }
x.report("File.join") { 1000000.times{File.join "tmp", "foo"} }
end
Calculating -------------------------------------
String::prepend 86847 i/100ms
File.join 57009 i/100ms
-------------------------------------------------
String::prepend 2287790.4 (±7.2%) i/s - 11376957 in 5.005150s
File.join 1189393.1 (±3.0%) i/s - 5985945 in 5.037805s
user system total real
String::prepend 0.350000 0.000000 0.350000 ( 0.352643)
File.join 0.760000 0.000000 0.760000 ( 0.759962)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment