Created
July 27, 2019 14:03
-
-
Save fcheung/1f986be62515ab34965a35f361e5efb5 to your computer and use it in GitHub Desktop.
unpack cookie benchmark
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/ips' | |
def reverse(session_data) | |
digest, session_data = session_data.reverse.split('--', 2) | |
digest.reverse! if digest | |
session_data.reverse! if session_data | |
[session_data, digest] | |
end | |
def index(session_data) | |
index = session_data.rindex('--') | |
[session_data[0, index], session_data[index+2, session_data.length - (index+2)]] | |
end | |
def rpartition(session_data) | |
session_data, _, digest = session_data.rpartition('--') | |
end | |
n = 10 | |
data = 'a'*512 + '--' + 'b'*32 | |
Benchmark.ips do |x| | |
x.report('reverse:') { n.times { reverse(data) } } | |
x.report('index:') { n.times { index(data) } } | |
x.report('rpartition:') { n.times { rpartition(data) } } | |
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
reverse: 64.886k (± 5.2%) i/s - 328.547k in 5.079341s | |
index: 121.035k (± 1.8%) i/s - 607.898k in 5.024200s | |
rpartition: 129.777k (± 3.0%) i/s - 651.896k in 5.027945s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment