Created
August 1, 2022 00:45
-
-
Save postmodern/83731685900fb851ee799026ba46e087 to your computer and use it in GitHub Desktop.
Micro-benchmark to test different ways of adding Arrays
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
#!/usr/bin/env ruby | |
require 'benchmark' | |
Benchmark.bm do |b| | |
n = 1_000_000 | |
array1 = [1,2,3,4,5,6] | |
array2 = [7,8,9,10,11,12,13,14] | |
array3 = [15,16,17,18,19,20] | |
b.report('array + array + ...') do | |
n.times do | |
array1 + array2 + array3 | |
end | |
end | |
b.report('[*array, *array, ...]') do | |
n.times do | |
[*array1, *array2, *array3] | |
end | |
end | |
end |
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
user system total real | |
array + array + ... 0.223602 0.000000 0.223602 ( 0.225337) | |
[*array, *array, ...] 0.384585 0.005859 0.390444 ( 0.393595) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If in place update is a viable option you could also use
concat
: