Last active
          March 11, 2022 07:35 
        
      - 
      
- 
        Save danini-the-panini/883baaa92c4dc4f8ed1a49134600576c to your computer and use it in GitHub Desktop. 
    reject! vs next
  
        
  
    
      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/ips' | |
| N = 1_000_000 | |
| ARRAY = N.times.map { rand } | |
| Benchmark.ips do |x| | |
| x.report("reject") do | |
| out = [] | |
| ARRAY.reject! { _1 >= 0.5 } | |
| ARRAY.each { out << _1 } | |
| end | |
| x.report("next") do | |
| out = [] | |
| ARRAY.each { next if _1 >= 0.5; out << _1 } | |
| end | |
| end | 
  
    
      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
    
  
  
    
  | Warming up -------------------------------------- | |
| reject 3.000 i/100ms | |
| next 5.000 i/100ms | |
| Calculating ------------------------------------- | |
| reject 36.357 (± 0.0%) i/s - 183.000 in 5.034273s | |
| next 58.647 (± 1.7%) i/s - 295.000 in 5.030860s | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment