Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Ruby Benchmark: Determining the last part of a URI
# gem install benchmark-ips
require "benchmark/ips"
uri = ""
Benchmark.ips do |x|"file.basename") { File.basename(uri) }"split('/').last") { uri.split("/").last }"split('/')[-1]") { uri.split("/")[-1] }!
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
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