11

Benchmarking Ruby 2.7.0's Numbered Parameters — 28 Feb 2020

 4 years ago
source link: https://blog.schembri.me/post/benchmarking-ruby-2.7.0-numbered-parameters/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Benchmarking Ruby 2.7.0's Numbered Parameters

Ruby 2.7.0 added numbered block parameters . There was some controversy which resulted in the initial syntax of @n changing to _n . I like the change.

This is what it looks like:

# regular named params
HASH.map { |a, b| [a, b] }

# new numbered params
HASH.map { [_1, _2] }

Anyway, I wondered if there was any performance impact in this change so I threw together a quick benchmark . Result: it’s just as fast as named params.

Warming up --------------------------------------
        named params   143.381k i/100ms
     numbered params   142.011k i/100ms
Calculating -------------------------------------
        named params      2.027M (± 1.6%) i/s -     10.180M in   5.022400s
     numbered params      1.996M (± 2.4%) i/s -     10.083M in   5.053749s

Comparison:
        named params:  2027451.7 i/s
     numbered params:  1996273.3 i/s - same-ish: difference falls within error

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK