Skip to content

Instantly share code, notes, and snippets.

@mactkg
Last active June 15, 2017 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mactkg/668e108fbf7ee6ae61464f8a7a6b7191 to your computer and use it in GitHub Desktop.
Save mactkg/668e108fbf7ee6ae61464f8a7a6b7191 to your computer and use it in GitHub Desktop.
(Ruby 2.2.2)
user system total real
Scale: 1
shuffle and take 0.010000 0.000000 0.010000 ( 0.007580)
sample 0.000000 0.000000 0.000000 ( 0.004182)
Scale: 10
shuffle and take 0.010000 0.000000 0.010000 ( 0.011985)
sample 0.020000 0.000000 0.020000 ( 0.016216)
Scale: 100
shuffle and take 0.040000 0.010000 0.050000 ( 0.052012)
sample 0.010000 0.000000 0.010000 ( 0.013686)
Scale: 1000
shuffle and take 0.360000 0.090000 0.450000 ( 0.597210)
sample 0.000000 0.000000 0.000000 ( 0.006961)
Scale: 10000
shuffle and take 2.080000 0.140000 2.220000 ( 2.247604)
sample 0.000000 0.000000 0.000000 ( 0.005969)
Scale: 100000
shuffle and take 22.120000 2.120000 24.240000 ( 24.661264)
sample 0.000000 0.000000 0.000000 ( 0.007182)
Scale: 1000000
shuffle and take 370.370000 22.020000 392.390000 (402.516775)
sample 0.010000 0.000000 0.010000 ( 0.007679)
require 'benchmark'
Benchmark.bm(10) do |x|
7.times do |n|
scale = 10 ** n
puts "\nScale: #{scale}"
data = Array.new(scale).map { Random.rand() }
x.report("shuffle and take") do
10000.times do
a = data.shuffle.take(10)
end
end
data = Array.new(scale).map { Random.rand() }
x.report("sample") do
10000.times do
a = data.sample(10)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment