Skip to content

Instantly share code, notes, and snippets.

@RespiteSage
Created July 16, 2021 18:53
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 RespiteSage/9f92a5e4505b932edd2cfd94f4d2f9fe to your computer and use it in GitHub Desktop.
Save RespiteSage/9f92a5e4505b932edd2cfd94f4d2f9fe to your computer and use it in GitHub Desktop.
Array vs StaticArray Benchmark
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) }
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) }
std_sorted_array_sm = Array(Int32).new(8) { rand(0..1000) }
std_sorted_array_md = Array(Int32).new(16) { rand(0..1000) }
std_sorted_array_lg = Array(Int32).new(32) { rand(0..1000) }
static_sorted_array_sm = StaticArray(Int32, 8).new { rand(0..1000) }
static_sorted_array_md = StaticArray(Int32, 16).new { rand(0..1000) }
static_sorted_array_lg = StaticArray(Int32, 32).new { rand(0..1000) }
tmp_int = 0
puts "#bsearch(&block : T -> Bool)"
Benchmark.ips do |x|
x.report("std_array_sm") { tmp_int &+= std_array_sm.bsearch { |x| x < 1 } || 0 }
x.report("std_array_md") { tmp_int &+= std_array_md.bsearch { |x| x < 1 } || 0 }
x.report("std_array_lg") { tmp_int &+= std_array_lg.bsearch { |x| x < 1 } || 0 }
x.report("static_array_sm") { tmp_int &+= static_array_sm.bsearch { |x| x < 1 } || 0 }
x.report("static_array_md") { tmp_int &+= static_array_md.bsearch { |x| x < 1 } || 0 }
x.report("static_array_lg") { tmp_int &+= static_array_lg.bsearch { |x| x < 1 } || 0 }
end
tmp_bool = false
puts "#all?(&)"
Benchmark.ips do |x|
x.report("std_array_sm") { tmp_bool ||= std_array_sm.all? { |x| -1 < x } }
x.report("std_array_md") { tmp_bool ||= std_array_md.all? { |x| -1 < x } }
x.report("std_array_lg") { tmp_bool ||= std_array_lg.all? { |x| -1 < x } }
x.report("static_array_sm") { tmp_bool ||= static_array_sm.all? { |x| -1 < x } }
x.report("static_array_md") { tmp_bool ||= static_array_md.all? { |x| -1 < x } }
x.report("static_array_lg") { tmp_bool ||= static_array_lg.all? { |x| -1 < x } }
end
puts "#count(&)"
Benchmark.ips do |x|
x.report("std_array_sm") { tmp_int &+= std_array_sm.count { |x| -1 < x } }
x.report("std_array_md") { tmp_int &+= std_array_md.count { |x| -1 < x } }
x.report("std_array_lg") { tmp_int &+= std_array_lg.count { |x| -1 < x } }
x.report("static_array_sm") { tmp_int &+= static_array_sm.count { |x| -1 < x } }
x.report("static_array_md") { tmp_int &+= static_array_md.count { |x| -1 < x } }
x.report("static_array_lg") { tmp_int &+= static_array_lg.count { |x| -1 < x } }
end
puts "#first"
Benchmark.ips do |x|
x.report("std_array_sm") { tmp_int &+= std_array_sm.first }
x.report("std_array_md") { tmp_int &+= std_array_md.first }
x.report("std_array_lg") { tmp_int &+= std_array_lg.first }
x.report("static_array_sm") { tmp_int &+= static_array_sm.first }
x.report("static_array_md") { tmp_int &+= static_array_md.first }
x.report("static_array_lg") { tmp_int &+= static_array_lg.first }
end
puts "#minmax"
Benchmark.ips do |x|
x.report("std_array_sm") { tmp_int &+= std_array_sm.minmax.first }
x.report("std_array_md") { tmp_int &+= std_array_md.minmax.first }
x.report("std_array_lg") { tmp_int &+= std_array_lg.minmax.first }
x.report("static_array_sm") { tmp_int &+= static_array_sm.minmax.first }
x.report("static_array_md") { tmp_int &+= static_array_md.minmax.first }
x.report("static_array_lg") { tmp_int &+= static_array_lg.minmax.first }
end
puts "#reduce(memo, &)"
Benchmark.ips do |x|
x.report("std_array_sm") { tmp_int &+= std_array_sm.reduce(0) { |acc, i| acc + i } }
x.report("std_array_md") { tmp_int &+= std_array_md.reduce(0) { |acc, i| acc + i } }
x.report("std_array_lg") { tmp_int &+= std_array_lg.reduce(0) { |acc, i| acc + i } }
x.report("static_array_sm") { tmp_int &+= static_array_sm.reduce(0) { |acc, i| acc + i } }
x.report("static_array_md") { tmp_int &+= static_array_md.reduce(0) { |acc, i| acc + i } }
x.report("static_array_lg") { tmp_int &+= static_array_lg.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