-
-
Notifications
You must be signed in to change notification settings - Fork 766
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1068 from laserlemon/random-order-performance
Use Array#shuffle for random spec ordering
- Loading branch information
Showing
3 changed files
with
67 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require "benchmark" | ||
|
||
# This benchmark demonstrates the speed of Array#shuffle versus sorting by | ||
# random numbers. This is in reference to ordering examples using the | ||
# --order=rand command line flag. Array#shuffle also respects seeded random via | ||
# Kernel.srand. | ||
|
||
LIST = (1..1_000).to_a.freeze | ||
|
||
Benchmark.bmbm do |x| | ||
x.report("sort_by") do | ||
1_000.times do | ||
LIST.sort_by { Kernel.rand(LIST.size) } | ||
end | ||
end | ||
|
||
x.report("shuffle") do | ||
1_000.times do | ||
LIST.shuffle | ||
end | ||
end | ||
end | ||
|
||
# Ruby 2.0 | ||
# | ||
# 21x over 100 list elements: | ||
# | ||
# user system total real | ||
# sort_by 0.080000 0.000000 0.080000 ( 0.074924) | ||
# shuffle 0.000000 0.000000 0.000000 ( 0.003535) | ||
# | ||
# 27x over 1,000 list elements: | ||
# | ||
# user system total real | ||
# sort_by 0.870000 0.000000 0.870000 ( 0.874661) | ||
# shuffle 0.030000 0.000000 0.030000 ( 0.031949) | ||
# | ||
# 31x over 10,000 list elements: | ||
# | ||
# user system total real | ||
# sort_by 10.690000 0.010000 10.700000 ( 10.695433) | ||
# shuffle 0.330000 0.010000 0.340000 ( 0.342375) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters