Skip to content

Instantly share code, notes, and snippets.

@briankung
Created October 7, 2018 14:51
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 briankung/0cd776c790556461cec4d14164eedf4a to your computer and use it in GitHub Desktop.
Save briankung/0cd776c790556461cec4d14164eedf4a to your computer and use it in GitHub Desktop.
Timing attack test
# $ gem install benchmark-ips
require 'benchmark/ips'
Benchmark.ips do |x|
x.report("long correct string: ") { "abfhbeufangi" == "abfhbeufangi" }
x.report("long mostly correct string: ") { "abfhbeufangx" == "abfhbeufangi" }
x.report("long incorrect string: ") { "bbfhbeufangi" == "abfhbeufangi" }
x.report("too short: ") { "bbfhbe" == "abfhbeufangi" }
x.report("too long: ") { "abfhbeufangibbfhbe" == "abfhbeufangi" }
some_string = "boobahbear"
x.report("same object: ") { some_string == some_string }
end
=begin result
Warming up --------------------------------------
long correct string:
242.992k i/100ms
long mostly correct string:
218.304k i/100ms
long incorrect string:
231.458k i/100ms
too short: 240.521k i/100ms
too long: 240.734k i/100ms
same object: 250.431k i/100ms
Calculating -------------------------------------
long correct string:
4.537M (±18.7%) i/s - 21.626M in 5.026472s
long mostly correct string:
3.087M (±25.8%) i/s - 13.971M in 5.018859s
long incorrect string:
4.078M (±25.8%) i/s - 18.054M in 5.043101s
too short: 5.139M (± 8.9%) i/s - 25.495M in 5.007632s
too long: 5.137M (± 9.8%) i/s - 25.518M in 5.023060s
same object: 8.476M (± 8.9%) i/s - 42.072M in 5.006357s
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment