ruby-i18n / i18n

Internationalization (i18n) library for Ruby
MIT License
986 stars 411 forks source link

Optimize pluralization logic in test data #697

Closed zachmargolis closed 1 month ago

zachmargolis commented 3 months ago

Benchmarking

Scipt

Benchmark script to confirm that Array#include? is slower than Range#cover? bigger than 5 items or so

require 'benchmark/ips'

randoms = 1_000.times.map { rand(1000) }

Benchmark.ips do |x|
  x.report("89 items Array#include?") do
    randoms.each do |r|
      [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99].include?(r)
    end
  end

  x.report("89 items Range#cover?") do
    randoms.each do |r|
      (11..99).cover?(r)
    end
  end

  x.compare!

  x.report("9 items Array#include?") do
    randoms.each do |r|
      [11, 12, 13, 14, 15, 16, 17, 18, 19].include?(r)
    end
  end

  x.report("9 items Range#cover?") do
    randoms.each do |r|
      (11..19).cover?(r)
    end
  end

  x.compare!

  x.report("5 items Array#include?") do
    randoms.each do |r|
      [5, 6, 7, 8, 9].include?(r)
    end
  end

  x.report("5 items Range#cover?") do
    randoms.each do |r|
      (5..9).cover?(r)
    end
  end

  x.compare!

  x.report("4 items Array#include?") do
    randoms.each do |r|
      [11, 12, 13, 14].include?(r)
    end
  end

  x.report("4 items Range#cover?") do
    randoms.each do |r|
      (11..14).cover?(r)
    end
  end

  x.compare!

  x.report("3 items Array#include?") do
    randoms.each do |r|
      [2, 3, 4].include?(r)
    end
  end

  x.report("3 items Range#cover?") do
    randoms.each do |r|
      (2..4).cover?(r)
    end
  end

  x.compare!
end

Results

... (warming up)
Calculating -------------------------------------
89 items Array#include?
                          3.037k (± 1.6%) i/s -     15.300k in   5.039326s
89 items Range#cover?
                         13.428k (± 0.4%) i/s -     67.626k in   5.036282s
9 items Array#include?
                         12.316k (± 2.6%) i/s -     62.350k in   5.066138s
9 items Range#cover?     12.964k (± 5.6%) i/s -     64.974k in   5.036638s
5 items Array#include?
                         14.430k (± 3.3%) i/s -     72.288k in   5.016121s
5 items Range#cover?     13.037k (± 0.8%) i/s -     66.200k in   5.078273s
4 items Array#include?
                         14.970k (± 1.5%) i/s -     75.072k in   5.015868s
4 items Range#cover?     13.069k (± 0.9%) i/s -     66.346k in   5.076939s
3 items Array#include?
                         16.580k (± 1.4%) i/s -     84.280k in   5.084254s
3 items Range#cover?     13.430k (± 0.9%) i/s -     68.000k in   5.063657s

Comparison:
3 items Array#include?:    16579.8 i/s
4 items Array#include?:    14970.3 i/s - 1.11x  slower
5 items Array#include?:    14430.3 i/s - 1.15x  slower
3 items Range#cover?:    13430.1 i/s - 1.23x  slower
89 items Range#cover?:    13428.0 i/s - 1.23x  slower
4 items Range#cover?:    13069.2 i/s - 1.27x  slower
5 items Range#cover?:    13036.7 i/s - 1.27x  slower
9 items Range#cover?:    12964.1 i/s - 1.28x  slower
9 items Array#include?:    12315.9 i/s - 1.35x  slower
89 items Array#include?:     3036.9 i/s - 5.46x  slower
radar commented 2 months ago

Thanks! Sorry it took me a while to get to this, I've been down sick.