Created
September 18, 2012 00:07
-
-
Save ahk/3740530 to your computer and use it in GitHub Desktop.
Do ruby strings leak timing info when comparing with == ?
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' | |
ace1 = 'a' + ('b' * 10000) | |
ace2 = 'a' + ('b' * 10000) | |
lastace = ('b' * 10000) + 'a' | |
bees = 'b' * 10001 | |
n = 10000000 | |
Benchmark.bmbm do |x| | |
x.report ('same str') { n.times { ace1 == ace2 } } | |
x.report ('first char diff') { n.times { ace1 == bees } } | |
x.report ('last char diff') { n.times { bees == lastace } } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment