Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace flatten to Object#is_a? #22

Merged
merged 1 commit into from Mar 27, 2018
Merged

Conversation

guppy0356
Copy link

Hi, I compare using flatten and using is_a? Using is_a? is faster than flatten.

Warming up --------------------------------------
             flatten     1.869k i/s
      using Array.[]    56.587k i/s
        Object#is_a?    16.534M i/s
Calculating -------------------------------------
             flatten     2.029k i/s -      5.607k times in 2.763675s (492.90μs/i)
      using Array.[]    54.798k i/s -    169.760k times in 3.097910s (18.25μs/i)
        Object#is_a?    30.039M i/s -     49.602M times in 1.651260s (33.29ns/i)

Comparison:
        Object#is_a?:  30038651.1 i/s 
      using Array.[]:     54798.2 i/s - 548.17x  slower
             flatten:      2028.8 i/s - 14805.97x  slower
require 'benchmark_driver'

Benchmark.driver do |x|
  x.prelude %{ array = [*1..5000] }
  x.report "flatten", %{ [array].flatten }
  x.report "using Array.[]", %{ Array[*array] }
  x.report "Object#is_a?", %{ array.is_a?(Array) ? array : [array] }
  x.compare!
end

@nfedyashev nfedyashev merged commit 3d7937d into nfedyashev:master Mar 27, 2018
@nfedyashev
Copy link
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants