Last active
July 16, 2021 17:57
-
-
Save jkthorne/eecb5c778c44ec21bb29a4aff8266de0 to your computer and use it in GitHub Desktop.
benchmark array v static array
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
require "benchmark" | |
std_array_sm = Array(Int32).new(8) { rand(0..1000) } | |
std_array_md = Array(Int32).new(16) { rand(0..1000) } | |
std_array_lg = Array(Int32).new(32) { rand(0..1000) } | |
std_array_xlg = Array(Int32).new(64) { rand(0..1000) } | |
std_array_xxlg = Array(Int32).new(128) { rand(0..1000) } | |
static_array_sm = StaticArray(Int32, 8).new { rand(0..1000) } | |
static_array_md = StaticArray(Int32, 16).new { rand(0..1000) } | |
static_array_lg = StaticArray(Int32, 32).new { rand(0..1000) } | |
static_array_xlg = StaticArray(Int32, 64).new { rand(0..1000) } | |
static_array_xxlg = StaticArray(Int32, 128).new { rand(0..1000) } | |
std_sorted_array_sm = Array(Int32).new(8) { rand(0..1000) }.sort | |
std_sorted_array_md = Array(Int32).new(16) { rand(0..1000) }.sort | |
std_sorted_array_lg = Array(Int32).new(32) { rand(0..1000) }.sort | |
std_sorted_array_xlg = Array(Int32).new(64) { rand(0..1000) }.sort | |
std_sorted_array_xxlg = Array(Int32).new(128) { rand(0..1000) }.sort | |
static_sorted_array_sm = StaticArray(Int32, 8).new { rand(0..1000) } | |
static_sorted_array_sm.to_slice.sort! | |
static_sorted_array_md = StaticArray(Int32, 16).new { rand(0..1000) } | |
static_sorted_array_md.to_slice.sort! | |
static_sorted_array_lg = StaticArray(Int32, 32).new { rand(0..1000) } | |
static_sorted_array_lg.to_slice.sort! | |
static_sorted_array_xlg = StaticArray(Int32, 64).new { rand(0..1000) } | |
static_sorted_array_xlg.to_slice.sort! | |
static_sorted_array_xxlg = StaticArray(Int32, 128).new { rand(0..1000) } | |
static_sorted_array_xxlg.to_slice.sort! | |
puts "#bsearch(&block : T -> Bool)" | |
Benchmark.ips do |x| | |
x.report("std_sorted_array_sm") { std_sorted_array_sm.bsearch { |x| x < 1} } | |
x.report("std_sorted_array_md") { std_sorted_array_md.bsearch { |x| x < 1} } | |
x.report("std_sorted_array_lg") { std_sorted_array_lg.bsearch { |x| x < 1} } | |
x.report("std_sorted_array_xlg") { std_sorted_array_xlg.bsearch { |x| x < 1} } | |
x.report("std_sorted_array_xxlg") { std_sorted_array_xxlg.bsearch { |x| x < 1} } | |
x.report("static_sorted_array_sm") { static_sorted_array_sm.bsearch { |x| x < 1} } | |
x.report("static_sorted_array_md") { static_sorted_array_md.bsearch { |x| x < 1} } | |
x.report("static_sorted_array_lg") { static_sorted_array_lg.bsearch { |x| x < 1} } | |
x.report("static_sorted_array_xlg") { static_sorted_array_xlg.bsearch { |x| x < 1} } | |
x.report("static_sorted_array_xxlg") { static_sorted_array_xxlg.bsearch { |x| x < 1} } | |
end | |
puts "#all?(&)" | |
Benchmark.ips do |x| | |
x.report("std_array_sm") { std_array_sm.all? { |x| -1 < x } } | |
x.report("std_array_md") { std_array_md.all? { |x| -1 < x } } | |
x.report("std_array_lg") { std_array_lg.all? { |x| -1 < x } } | |
x.report("std_array_xlg") { std_array_xlg.all? { |x| -1 < x } } | |
x.report("std_array_xxlg") { std_array_xxlg.all? { |x| -1 < x } } | |
x.report("static_array_sm") { static_array_sm.all? { |x| -1 < x } } | |
x.report("static_array_md") { static_array_md.all? { |x| -1 < x } } | |
x.report("static_array_lg") { static_array_lg.all? { |x| -1 < x } } | |
x.report("static_array_xlg") { static_array_xlg.all? { |x| -1 < x } } | |
x.report("static_array_xxlg") { static_array_xxlg.all? { |x| -1 < x } } | |
end | |
puts "#count(&)" | |
Benchmark.ips do |x| | |
x.report("std_array_sm") { std_array_sm.count { |x| -1 < x } } | |
x.report("std_array_md") { std_array_md.count { |x| -1 < x } } | |
x.report("std_array_lg") { std_array_lg.count { |x| -1 < x } } | |
x.report("std_array_xlg") { std_array_xlg.count { |x| -1 < x } } | |
x.report("std_array_xxlg") { std_array_xxlg.count { |x| -1 < x } } | |
x.report("static_array_sm") { static_array_sm.count { |x| -1 < x } } | |
x.report("static_array_md") { static_array_md.count { |x| -1 < x } } | |
x.report("static_array_lg") { static_array_lg.count { |x| -1 < x } } | |
x.report("static_array_xlg") { static_array_xlg.count { |x| -1 < x } } | |
x.report("static_array_xxlg") { static_array_xxlg.count { |x| -1 < x } } | |
end | |
puts "#first" | |
Benchmark.ips do |x| | |
x.report("std_array_sm") { std_array_sm.first } | |
x.report("std_array_md") { std_array_md.first } | |
x.report("std_array_lg") { std_array_lg.first } | |
x.report("std_array_xlg") { std_array_xlg.first } | |
x.report("std_array_xxlg") { std_array_xxlg.first } | |
x.report("static_array_sm") { static_array_sm.first } | |
x.report("static_array_md") { static_array_md.first } | |
x.report("static_array_lg") { static_array_lg.first } | |
x.report("static_array_xlg") { static_array_xlg.first } | |
x.report("static_array_xxlg") { static_array_xxlg.first } | |
end | |
puts "#minmax" | |
Benchmark.ips do |x| | |
x.report("std_array_sm") { std_array_sm.minmax } | |
x.report("std_array_md") { std_array_md.minmax } | |
x.report("std_array_lg") { std_array_lg.minmax } | |
x.report("std_array_xlg") { std_array_xlg.minmax } | |
x.report("std_array_xxlg") { std_array_xxlg.minmax } | |
x.report("static_array_sm") { static_array_sm.minmax } | |
x.report("static_array_md") { static_array_md.minmax } | |
x.report("static_array_lg") { static_array_lg.minmax } | |
x.report("static_array_xlg") { static_array_xlg.minmax } | |
x.report("static_array_xxlg") { static_array_xxlg.minmax } | |
end | |
puts "#reduce(memo, &)" | |
Benchmark.ips do |x| | |
x.report("std_array_sm") { std_array_sm.reduce(0) { |acc, i| acc + i } } | |
x.report("std_array_md") { std_array_md.reduce(0) { |acc, i| acc + i } } | |
x.report("std_array_lg") { std_array_lg.reduce(0) { |acc, i| acc + i } } | |
x.report("std_array_xlg") { std_array_xlg.reduce(0) { |acc, i| acc + i } } | |
x.report("std_array_xxlg") { std_array_xxlg.reduce(0) { |acc, i| acc + i } } | |
x.report("static_array_sm") { static_array_sm.reduce(0) { |acc, i| acc + i } } | |
x.report("static_array_md") { static_array_md.reduce(0) { |acc, i| acc + i } } | |
x.report("static_array_lg") { static_array_lg.reduce(0) { |acc, i| acc + i } } | |
x.report("static_array_xlg") { static_array_xlg.reduce(0) { |acc, i| acc + i } } | |
x.report("static_array_xxlg") { static_array_xxlg.reduce(0) { |acc, i| acc + i } } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment