/bench.cr Secret
Created
March 10, 2021 17:21
Slice#hexdump(io : IO)
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" | |
struct Slice(T) | |
def hexdump_old | |
# definition before this PR | |
end | |
end | |
COUNT = 10000 | |
def test_n(n) | |
Benchmark.ips do |b| | |
b.report("Slice#hexdump") do | |
bytes = Random::DEFAULT.random_bytes(n) | |
COUNT.times { bytes.hexdump } | |
end | |
b.report("Slice#hexdump_old") do | |
bytes = Random::DEFAULT.random_bytes(n) | |
COUNT.times { bytes.hexdump_old } | |
end | |
b.report("Slice#hexdump(IO)") do | |
bytes = Random::DEFAULT.random_bytes(n) | |
io = IO::Memory.new | |
COUNT.times { io.rewind; bytes.hexdump(io) } | |
end | |
b.report("IO#puts hexdump") do | |
bytes = Random::DEFAULT.random_bytes(n) | |
io = IO::Memory.new | |
COUNT.times { io.rewind; io.puts bytes.hexdump } | |
end | |
b.report("String.build") do | |
bytes = Random::DEFAULT.random_bytes(n) | |
COUNT.times { String.build { |io| bytes.hexdump(io) } } | |
end | |
b.report("String.build w/ cap") do | |
bytes = Random::DEFAULT.random_bytes(n) | |
cap = (n / 16.0).ceil.to_i * 77 | |
COUNT.times { String.build(cap) { |io| bytes.hexdump(io) } } | |
end | |
end | |
end | |
{0, 1, 8, 20, 80, 300, 1500}.each do |n| | |
puts "N = #{n}:" | |
test_n(n) | |
puts | |
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
N = 0: | |
Slice#hexdump 27.89k ( 35.85µs) (± 5.80%) 16.0B/op 8.35× slower | |
Slice#hexdump_old 229.72k ( 4.35µs) (± 4.96%) 16.0B/op 1.01× slower | |
Slice#hexdump(IO) 232.85k ( 4.29µs) (± 1.79%) 192B/op fastest | |
IO#puts hexdump 7.58k (131.94µs) (± 3.67%) 192B/op 30.72× slower | |
String.build 967.16 ( 1.03ms) (± 6.99%) 1.83MB/op 240.76× slower | |
String.build w/ cap 1.38k (724.95µs) (± 5.00%) 1.07MB/op 168.81× slower | |
N = 1: | |
Slice#hexdump 717.97 ( 1.39ms) (± 3.92%) 781kB/op 1.46× slower | |
Slice#hexdump_old 428.11 ( 2.34ms) (± 3.37%) 781kB/op 2.46× slower | |
Slice#hexdump(IO) 1.05k (950.76µs) (± 4.19%) 192B/op fastest | |
IO#puts hexdump 677.17 ( 1.48ms) (± 5.78%) 781kB/op 1.55× slower | |
String.build 473.76 ( 2.11ms) (± 4.43%) 1.68MB/op 2.22× slower | |
String.build w/ cap 454.08 ( 2.20ms) (± 4.19%) 1.83MB/op 2.32× slower | |
N = 8: | |
Slice#hexdump 621.87 ( 1.61ms) (± 4.08%) 938kB/op 1.43× slower | |
Slice#hexdump_old 446.16 ( 2.24ms) (± 4.29%) 938kB/op 1.99× slower | |
Slice#hexdump(IO) 887.06 ( 1.13ms) (± 2.32%) 336B/op fastest | |
IO#puts hexdump 580.32 ( 1.72ms) (± 4.47%) 938kB/op 1.53× slower | |
String.build 341.81 ( 2.93ms) (± 4.85%) 3.05MB/op 2.60× slower | |
String.build w/ cap 423.19 ( 2.36ms) (± 5.11%) 1.83MB/op 2.10× slower | |
N = 20: | |
Slice#hexdump 335.19 ( 2.98ms) (± 4.41%) 1.53MB/op 1.28× slower | |
Slice#hexdump_old 244.35 ( 4.09ms) (± 3.89%) 1.53MB/op 1.76× slower | |
Slice#hexdump(IO) 429.21 ( 2.33ms) (± 2.89%) 624B/op fastest | |
IO#puts hexdump 321.07 ( 3.11ms) (± 4.46%) 1.53MB/op 1.34× slower | |
String.build 191.42 ( 5.22ms) (± 7.70%) 5.65MB/op 2.24× slower | |
String.build w/ cap 256.97 ( 3.89ms) (± 5.67%) 2.59MB/op 1.67× slower | |
N = 80: | |
Slice#hexdump 126.07 ( 7.93ms) (± 4.61%) 4.27MB/op 1.23× slower | |
Slice#hexdump_old 109.69 ( 9.12ms) (± 4.83%) 4.27MB/op 1.42× slower | |
Slice#hexdump(IO) 155.38 ( 6.44ms) (± 2.21%) 1.33kB/op fastest | |
IO#puts hexdump 123.49 ( 8.10ms) (± 3.77%) 4.27MB/op 1.26× slower | |
String.build 84.33 ( 11.86ms) (± 4.31%) 12.0MB/op 1.84× slower | |
String.build w/ cap 111.95 ( 8.93ms) (± 4.47%) 5.19MB/op 1.39× slower | |
N = 300: | |
Slice#hexdump 35.44 ( 28.21ms) (± 4.43%) 19.6MB/op 1.17× slower | |
Slice#hexdump_old 30.15 ( 33.17ms) (± 4.87%) 19.6MB/op 1.38× slower | |
Slice#hexdump(IO) 41.64 ( 24.01ms) (± 2.47%) 4.9kB/op fastest | |
IO#puts hexdump 34.93 ( 28.63ms) (± 4.48%) 19.6MB/op 1.19× slower | |
String.build 26.24 ( 38.11ms) (± 4.02%) 64.1MB/op 1.59× slower | |
String.build w/ cap 31.91 ( 31.34ms) (± 3.75%) 20.4MB/op 1.30× slower | |
N = 1500: | |
Slice#hexdump 7.92 (126.26ms) (± 4.28%) 69.1MB/op 1.05× slower | |
Slice#hexdump_old 6.69 (149.47ms) (± 3.25%) 69.1MB/op 1.24× slower | |
Slice#hexdump(IO) 8.29 (120.69ms) (± 2.31%) 18.6kB/op fastest | |
IO#puts hexdump 7.22 (138.48ms) (± 4.93%) 69.1MB/op 1.15× slower | |
String.build 6.40 (156.15ms) (± 2.65%) 162MB/op 1.29× slower | |
String.build w/ cap 6.21 (161.13ms) (±14.69%) 70.2MB/op 1.34× slower |
Author
HertzDevil
commented
Mar 10, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment