Skip to content

Instantly share code, notes, and snippets.

@dominikh
Created October 12, 2009 02:21
Show Gist options
  • Save dominikh/208069 to your computer and use it in GitHub Desktop.
Save dominikh/208069 to your computer and use it in GitHub Desktop.

Revisions

  1. dominikh created this gist Oct 12, 2009.
    37 changes: 37 additions & 0 deletions regexp-match.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    require 'benchmark'

    TIMES = 1_000_000

    r = /regexp/
    s = "regexp"

    Benchmark.bmbm do |x|
    x.report("r =~ s") do
    TIMES.times { r =~ s }
    end

    x.report("s =~ r") do
    TIMES.times { s =~ r }
    end

    x.report("r.match(s)") do
    TIMES.times { r.match(s) }
    end

    x.report("s.match(r)") do
    TIMES.times { s.match(r) }
    end
    end

    # Rehearsal ----------------------------------------------
    # r =~ s 1.210000 0.000000 1.210000 ( 1.207980)
    # s =~ r 1.420000 0.010000 1.430000 ( 1.428233)
    # r.match(s) 2.560000 0.000000 2.560000 ( 2.568816)
    # s.match(r) 2.960000 0.000000 2.960000 ( 2.961407)
    # ------------------------------------- total: 8.160000sec

    # user system total real
    # r =~ s 1.190000 0.010000 1.200000 ( 1.193561)
    # s =~ r 1.220000 0.000000 1.220000 ( 1.218504)
    # r.match(s) 2.780000 0.000000 2.780000 ( 2.789282)
    # s.match(r) 2.640000 0.000000 2.640000 ( 2.646546)