プロジェクト

全般

プロフィール

Vote #81107

完了

Use sum instead of inject(0, :+)

Admin Redmine さんが3年以上前に追加. 3年以上前に更新.

ステータス:
Closed
優先度:
通常
担当者:
-
カテゴリ:
Performance_53
対象バージョン:
開始日:
2022/05/09
期日:
進捗率:

0%

予定工数:
category_id:
53
version_id:
152
issue_org_id:
34153
author_id:
332
assigned_to_id:
332
comments:
5
status_id:
5
tracker_id:
3
plus1:
0
affected_version:
closed_on:
affected_version_id:
ステータス-->[Closed]

説明

@Array#sum@ introduced in Ruby 2.4 is much faster than @Enumerable#inject@.

require 'benchmark/ips'

numbers = (1..10).to_a

Benchmark.ips do |x|
  x.report('inject') do
    numbers.inject(0, :+)
  end
  x.report('sum') do
    numbers.sum
  end
  x.compare!
end
Warming up --------------------------------------
              inject   169.646k i/100ms
                 sum   234.947k i/100ms
Calculating -------------------------------------
              inject      1.692M (± 1.7%) i/s -      8.482M in   5.013728s
                 sum      2.347M (± 0.9%) i/s -     11.747M in   5.006572s

Comparison:
                 sum:  2346592.2 i/s
              inject:  1692335.1 i/s - 1.39x  (± 0.00) slower

I have generated the attached patch with rubocop-performance 1.8.1 (Performance/Sum).


journals

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
Committed the patch.
--------------------------------------------------------------------------------


related_issues

relates,Closed,34142,Drop Ruby 2.3 support

Admin Redmine さんが3年以上前に更新

  • カテゴリPerformance_53 にセット
  • 対象バージョン4.2.0_152 にセット

他の形式にエクスポート: Atom PDF

いいね!0
いいね!0