Created
          December 12, 2013 00:45 
        
      - 
      
- 
        Save pjb3/7921351 to your computer and use it in GitHub Desktop. 
    A benchmark of calling methods in different ways
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | require "benchmark" | |
| test = "hi man" | |
| m = test.method(:length) | |
| n = 100000 | |
| Benchmark.bmbm {|x| | |
| x.report("meth") { n.times { test.length } } | |
| x.report("call") { n.times { m.call } } | |
| x.report("send") { n.times { test.send(:length) } } | |
| x.report("eval") { n.times { eval "test.length" } } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 2.0.0-p353 ~ $ ruby bench.rb | |
| Rehearsal ---------------------------------------- | |
| meth 0.000000 0.000000 0.000000 ( 0.005811) | |
| call 0.010000 0.000000 0.010000 ( 0.010587) | |
| send 0.010000 0.000000 0.010000 ( 0.009120) | |
| eval 0.610000 0.060000 0.670000 ( 0.672308) | |
| ------------------------------- total: 0.690000sec | |
| user system total real | |
| meth 0.010000 0.000000 0.010000 ( 0.005437) | |
| call 0.010000 0.000000 0.010000 ( 0.010529) | |
| send 0.010000 0.000000 0.010000 ( 0.009405) | |
| eval 0.560000 0.030000 0.590000 ( 0.587010) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment