Skip to content

Instantly share code, notes, and snippets.

@zanker
Created September 28, 2015 03:48
Show Gist options
  • Save zanker/be46e264f1632fe0cb17 to your computer and use it in GitHub Desktop.
Save zanker/be46e264f1632fe0cb17 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'protobuf'
require 'varint/varint'
ITERATIONS = 1_000_000
class Test
extend ::Varint
def self.pure_alt(io)
int_val = 0
shift = 0
loop do
byte = io.getbyte
int_val |= (byte & 0b0111_1111) << shift
shift += 7
return int_val if (byte & 0b1000_0000) == 0
end
end
end
Benchmark.bm do |x|
x.report(:pure_protobuf) do
ITERATIONS.times do
Protobuf::Decoder.read_varint(StringIO.new("9_999_999_999_999_999_999"))
end
end
x.report(:pure_alt) do
ITERATIONS.times do
Test.pure_alt(StringIO.new("9_999_999_999_999_999_999"))
end
end
x.report(:c) do
ITERATIONS.times do
Test.decode(StringIO.new("9_999_999_999_999_999_999"))
end
end
end
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment