Skip to content

Instantly share code, notes, and snippets.

@koic
Created July 14, 2017 10:17
Show Gist options
  • Save koic/bb85f03a534edffc6a52fcdc66e47568 to your computer and use it in GitHub Desktop.
Save koic/bb85f03a534edffc6a52fcdc66e47568 to your computer and use it in GitHub Desktop.
Is `casecmp?` faster? -> No
require 'benchmark/ips'
Benchmark.ips do |x|
x.report('upcase') { 'String'.upcase == 'string' }
x.report('downcase') { 'String'.downcase == 'string' }
x.report('casecmp') { 'String'.casecmp('string').zero? }
x.report('casecmp?') { 'String'.casecmp?('string') }
x.compare!
end
Warming up --------------------------------------
upcase 202.310k i/100ms
downcase 209.096k i/100ms
casecmp 225.472k i/100ms
casecmp? 177.762k i/100ms
Calculating -------------------------------------
upcase 3.841M (± 5.4%) i/s - 19.219M in 5.021148s
downcase 3.860M (± 5.7%) i/s - 19.237M in 5.001815s
casecmp 4.411M (± 6.6%) i/s - 22.096M in 5.035209s
casecmp? 3.076M (± 3.2%) i/s - 15.465M in 5.033448s
Comparison:
casecmp: 4410630.9 i/s
downcase: 3860071.1 i/s - 1.14x slower
upcase: 3840503.1 i/s - 1.15x slower
casecmp?: 3075894.5 i/s - 1.43x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment