GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — reverse.detect vs select { ... }.last · Issue #98 · fastruby/fast-ruby

reverse.detect vs select { ... }.last #98

New issue
Copy link
New issue
Copy link
Open
Open
reverse.detect vs select { ... }.last#98
Copy link

Description

@nerdrew
nerdrew
opened on Feb 11, 2016
Issue body actions

Third approach?

require 'benchmark/ips'

ARRAY = [*1..100]

def faster
  ARRAY.reverse_each { |x| break x if (x % 10).zero? }
end

def fast
  ARRAY.reverse.detect { |x| (x % 10).zero? }
end

def slow
  ARRAY.select { |x| (x % 10).zero? }.last
end

Benchmark.ips do |x|
  x.report('Enumerable#reverse_each + break') { faster }
  x.report('Enumerable#reverse.detect') { fast }
  x.report('Enumerable#select.last')    { slow }
  x.compare!
end
% ruby reverse.rb
Calculating -------------------------------------
Enumerable#reverse_each + break
                       115.335k i/100ms
Enumerable#reverse.detect
                        72.531k i/100ms
Enumerable#select.last
                        11.243k i/100ms
-------------------------------------------------
Enumerable#reverse_each + break
                          3.311M (± 7.2%) i/s -     16.493M
Enumerable#reverse.detect
                          1.255M (± 9.3%) i/s -      6.238M
Enumerable#select.last
                        129.592k (± 3.4%) i/s -    652.094k

Comparison:
Enumerable#reverse_each + break:  3310776.9 i/s
Enumerable#reverse.detect:  1255082.9 i/s - 2.64x slower
Enumerable#select.last:   129592.0 i/s - 25.55x slower

Activity

  1. PeterCamilleri commented on Feb 11, 2016

    @PeterCamilleri
    PeterCamilleri
    on Feb 11, 2016
    More actions

    This test is biased in favor of reverse each because it bails on the first element, 100. A fairer test would be to search for (x % 51).zero?, since we expect to traverse about half the elements on average.

  2. nerdrew commented on Feb 12, 2016

    @nerdrew
    nerdrew
    on Feb 12, 2016
    Author
    More actions

    Results with x % 51

    Comparison:
    Enumerable#reverse_each + break:   257525.8 i/s
    Enumerable#reverse.detect:   189938.5 i/s - 1.36x slower
    Enumerable#select.last:   129624.6 i/s - 1.99x slower
    
  3. PeterCamilleri commented on Feb 12, 2016

    @PeterCamilleri
    PeterCamilleri
    on Feb 12, 2016
    More actions

    Wow. That sure seems to level the playing field.

  4. parkerfinch commented on Apr 11, 2018

    @parkerfinch
    parkerfinch
    on Apr 11, 2018
    Contributor
    More actions

    I'd propose replacing reverse.detect with reverse_each.detect in the comparison, rather than adding it as a third option, for all the reasons listed in the reverse.each vs reverse_each section.

    If there's support behind this, I'm happy to whip up a PR for it!

  5. corsonknowles commented on Sep 21, 2020

    @corsonknowles
    corsonknowles
    on Sep 21, 2020
    More actions

    Yes, reverse_each.detect and reverse_each.any? are going to be more performant than select.last or reverse.detect.
    This is even documented in another benchmark on the same page. But reference material by definition usually means you look at the one relevant entry.

    It's worth an update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.