Skip to content

Instantly share code, notes, and snippets.

@guilhermesimoes
Last active November 30, 2016 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilhermesimoes/148cd905a99289441e5c46c991daa469 to your computer and use it in GitHub Desktop.
Save guilhermesimoes/148cd905a99289441e5c46c991daa469 to your computer and use it in GitHub Desktop.
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