Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby Benchmark: Determining the last part of a URI
# gem install benchmark-ips
require "benchmark/ips"
uri = "http://twitter.com/user/statuses/1234567891011121314"
Benchmark.ips do |x|
x.report("file.basename") { File.basename(uri) }
x.report("split('/').last") { uri.split("/").last }
x.report("split('/')[-1]") { uri.split("/")[-1] }
x.compare!
end
Warming up --------------------------------------
file.basename 68.360k i/100ms
split('/').last 37.097k i/100ms
split('/')[-1] 44.238k i/100ms
Calculating -------------------------------------
file.basename 863.262k (±12.9%) i/s - 4.238M in 5.006200s
split('/').last 603.615k (± 9.0%) i/s - 3.005M in 5.027355s
split('/')[-1] 572.872k (± 9.7%) i/s - 2.875M in 5.070618s
Comparison:
file.basename: 863262.4 i/s
split('/').last: 603615.0 i/s - 1.43x slower
split('/')[-1]: 572872.0 i/s - 1.51x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment