-
-
Notifications
You must be signed in to change notification settings - Fork 81
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
Improve Performance/SortReverse
cop
#382
Comments
Dockerfile
Benchmarkrequire 'benchmark/ips'
require 'bundler/inline'
require 'securerandom'
Foobar = Struct.new(:foo, keyword_init: true)
ARR10 = 10.times.map { Foobar.new(foo: rand(1000)) }
ARR1000 = 1000.times.map { Foobar.new(foo: rand(1000)) }
ARR1000000 = 1000000.times.map { Foobar.new(foo: rand(1000)) }
ARR_HEXES = 100.times.map { Foobar.new(foo: SecureRandom.hex(13)) }
puts "======== Hexes ========"
Benchmark.ips do |x|
x.report("sort_by(...).reverse") { ARR_HEXES.sort_by(&:foo).reverse }
x.report("sort { ... }") { ARR_HEXES.sort { |x, y| y.foo <=> x.foo } }
x.compare!
end
puts "======== 10 ========"
Benchmark.ips do |x|
x.report("sort_by(...).reverse") { ARR10.sort_by(&:foo).reverse }
x.report("sort { ... }") { ARR10.sort { |x, y| y.foo <=> x.foo } }
x.compare!
end
puts "======== 1000 ========"
Benchmark.ips do |x|
x.report("sort_by(...).reverse") { ARR1000.sort_by(&:foo).reverse }
x.report("sort { ... }") { ARR1000.sort { |x, y| y.foo <=> x.foo } }
x.compare!
end
puts "======== 1_000_000 ========"
Benchmark.ips do |x|
x.report("sort_by(...).reverse") { ARR1000000.sort_by(&:foo).reverse }
x.report("sort { ... }") { ARR1000000.sort { |x, y| y.foo <=> x.foo } }
x.compare!
end Resultydakuka@yauhenid:~/ruby-docker-app$ docker run ruby-app
======== Hexes ========
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-linux]
Warming up --------------------------------------
sort_by(...).reverse 5.052k i/100ms
sort { ... } 2.044k i/100ms
Calculating -------------------------------------
sort_by(...).reverse 48.109k (± 2.7%) i/s - 242.496k in 5.044237s
sort { ... } 18.766k (± 3.2%) i/s - 94.024k in 5.015877s
Comparison:
sort_by(...).reverse: 48109.4 i/s
sort { ... }: 18766.2 i/s - 2.56x slower
======== 10 ========
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-linux]
Warming up --------------------------------------
sort_by(...).reverse 69.348k i/100ms
sort { ... } 47.364k i/100ms
Calculating -------------------------------------
sort_by(...).reverse 682.383k (± 1.3%) i/s - 3.467M in 5.082126s
sort { ... } 457.078k (± 1.7%) i/s - 2.321M in 5.079091s
Comparison:
sort_by(...).reverse: 682383.2 i/s
sort { ... }: 457077.6 i/s - 1.49x slower
======== 1000 ========
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-linux]
Warming up --------------------------------------
sort_by(...).reverse 783.000 i/100ms
sort { ... } 134.000 i/100ms
Calculating -------------------------------------
sort_by(...).reverse 8.017k (± 6.0%) i/s - 39.933k in 5.001294s
sort { ... } 1.334k (± 1.8%) i/s - 6.700k in 5.025481s
Comparison:
sort_by(...).reverse: 8016.7 i/s
sort { ... }: 1333.7 i/s - 6.01x slower
======== 1_000_000 ========
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-linux]
Warming up --------------------------------------
sort_by(...).reverse 1.000 i/100ms
sort { ... } 1.000 i/100ms
Calculating -------------------------------------
sort_by(...).reverse 6.387 (±15.7%) i/s - 32.000 in 5.146557s
sort { ... } 0.528 (± 0.0%) i/s - 3.000 in 5.678265s
Comparison:
sort_by(...).reverse: 6.4 i/s
sort { ... }: 0.5 i/s - 12.09x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://docs.rubocop.org/rubocop-performance/cops_performance.html#performancesortreverse
Implemented
Not implemented
The text was updated successfully, but these errors were encountered: