Created
May 24, 2016 13:01
-
-
Save groyoh/4b1b1be7a1de4f4efbfa68ac2a422dba to your computer and use it in GitHub Desktop.
Set vs Regex
This file contains hidden or 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 "bundler/inline" | |
| gemfile(true) do | |
| source "https://rubygems.org" | |
| gem "benchmark-ips" | |
| end | |
| require "set" | |
| GC.start | |
| GC.disable | |
| S = Set["com.mysite.blabla.foo.bar", "com.mysite.hehe.foo.bar"] | |
| P = /com.mysite.(blabla|hehe).foo.bar/ | |
| INPUT = [ | |
| "fr.groyoh.fake", | |
| "com.matz.mri", | |
| "com.jruby.www.truffle", | |
| "com.com.com", | |
| "com.anothersite.blabla.foo", | |
| "com.mysite.blabla.foo.bar", | |
| "com.mysite.hehe.foo.bar" | |
| ] | |
| def rand | |
| INPUT.sample | |
| end | |
| Benchmark.ips do |x| | |
| #x.warmup = 20 | |
| x.report("REGEX") do | |
| P =~ rand | |
| end | |
| x.report("Set") do | |
| S.include? rand | |
| end | |
| x.compare! | |
| end | |
| __END__ | |
| Warming up -------------------------------------- | |
| REGEX 90.125k i/100ms | |
| Set 151.577k i/100ms | |
| Calculating ------------------------------------- | |
| REGEX 2.029M (± 9.5%) i/s - 10.094M in 5.026880s | |
| Set 4.371M (± 8.5%) i/s - 21.676M in 5.003899s | |
| Comparison: | |
| Set: 4370507.8 i/s | |
| REGEX: 2028924.8 i/s - 2.15x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment