Skip to content

Instantly share code, notes, and snippets.

@ahk
Created September 18, 2012 00:07
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 ahk/3740530 to your computer and use it in GitHub Desktop.
Save ahk/3740530 to your computer and use it in GitHub Desktop.
Do ruby strings leak timing info when comparing with == ?
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