Skip to content
  • schneems's avatar
    e1a72606
    Use block variable instead of global · e1a72606
    schneems authored
    
    ```ruby
    require 'benchmark/ips'
    
    Benchmark.ips do |x|
      x.report("$&") {
        "foo".sub(/f/) { $&.upcase }
      }
      x.report("block var") {
        "foo".sub(/f/) {|match| match.upcase }
      }
    end
    
    ```
    
    ```
    Calculating -------------------------------------
                      $&    48.658k i/100ms
               block var    49.666k i/100ms
    -------------------------------------------------
                      $&    873.156k (± 9.3%) i/s -      4.331M
               block var    969.744k (± 9.2%) i/s -      4.818M
    ```
    
    It's faster, and gets rid of a few "magic" global variables
    e1a72606
    Use block variable instead of global
    schneems authored
    
    ```ruby
    require 'benchmark/ips'
    
    Benchmark.ips do |x|
      x.report("$&") {
        "foo".sub(/f/) { $&.upcase }
      }
      x.report("block var") {
        "foo".sub(/f/) {|match| match.upcase }
      }
    end
    
    ```
    
    ```
    Calculating -------------------------------------
                      $&    48.658k i/100ms
               block var    49.666k i/100ms
    -------------------------------------------------
                      $&    873.156k (± 9.3%) i/s -      4.331M
               block var    969.744k (± 9.2%) i/s -      4.818M
    ```
    
    It's faster, and gets rid of a few "magic" global variables
Loading