Skip to content

Instantly share code, notes, and snippets.

@guilhermesimoes
Last active November 30, 2016 00:28
Show Gist options
  • Select an option

  • Save guilhermesimoes/148cd905a99289441e5c46c991daa469 to your computer and use it in GitHub Desktop.

Select an option

Save guilhermesimoes/148cd905a99289441e5c46c991daa469 to your computer and use it in GitHub Desktop.

Revisions

  1. guilhermesimoes created this gist Nov 25, 2016.
    15 changes: 15 additions & 0 deletions last_uri_path_benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    # 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
    14 changes: 14 additions & 0 deletions results
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    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