Created
          March 9, 2012 18:09 
        
      - 
      
 - 
        
Save gonz/2007820 to your computer and use it in GitHub Desktop.  
    Set/Array union/intersection benchmark
  
        
  
    
      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' | |
| require 'set' | |
| n = 50000 | |
| m = 5000 | |
| t = 100 | |
| r1 = (1..n).step(3) | |
| r2 = (1..m).step(2) | |
| s1 = Set.new(r1) | |
| s2 = Set.new(r2) | |
| a1 = r1.to_a | |
| a2 = r2.to_a | |
| Benchmark.bm(10) do |x| | |
| GC.start | |
| x.report('set union') { t.times{s1.union(s2)} } | |
| GC.start | |
| x.report('array union') { t.times{a1 | a2} } | |
| GC.start | |
| x.report('set int') { t.times{s1.intersection(s2)} } | |
| GC.start | |
| x.report('array int') { t.times{a1 & a2} } | |
| end | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment