Skip to content

Instantly share code, notes, and snippets.

@jodosha
Created April 15, 2016 13:12
Show Gist options
  • Select an option

  • Save jodosha/db1cf2696e6a4b84a95a7ab4853c5a92 to your computer and use it in GitHub Desktop.

Select an option

Save jodosha/db1cf2696e6a4b84a95a7ab4853c5a92 to your computer and use it in GitHub Desktop.
Benchmark for Hanami::Utils::Kernel.numeric?
#!/usr/bin/env ruby
require 'benchmark/ips'
module Hanami
module Utils
module Kernel
NUMERIC_MATCHER = /\A([\d\/\.\+iE]+|NaN|Infinity)\z/
def self.numeric?(arg)
arg.to_s.match(NUMERIC_MATCHER)
end
def self.double_negation_numeric?(arg)
!!arg.to_s.match(NUMERIC_MATCHER)
end
def self.not_nil_numeric?(arg)
!arg.to_s.match(NUMERIC_MATCHER).nil?
end
end
end
end
Benchmark.ips do |x|
x.report('current') { Hanami::Utils::Kernel.numeric?(23) }
x.report('double !!') { Hanami::Utils::Kernel.double_negation_numeric?(23) }
x.report('!nil?') { Hanami::Utils::Kernel.not_nil_numeric?(23) }
x.compare!
end
__END__
Results:
MRI 2.3
Warming up --------------------------------------
current 53.782k i/100ms
double !! 54.268k i/100ms
!nil? 54.237k i/100ms
Calculating -------------------------------------
current 848.395k (± 5.8%) i/s - 4.249M
double !! 834.871k (± 4.5%) i/s - 4.179M
!nil? 826.867k (± 4.3%) i/s - 4.176M
Comparison:
current: 848395.1 i/s
double !!: 834870.6 i/s - same-ish: difference falls within error
!nil?: 826866.9 i/s - same-ish: difference falls within error
MRI 2.2
Warming up --------------------------------------
current 66.096k i/100ms
double !! 66.132k i/100ms
!nil? 65.040k i/100ms
Calculating -------------------------------------
current 808.732k (± 4.6%) i/s - 4.098M in 5.078148s
double !! 804.129k (± 4.6%) i/s - 4.034M in 5.028391s
!nil? 786.999k (± 4.7%) i/s - 3.967M in 5.052622s
Comparison:
current: 808731.8 i/s
double !!: 804128.9 i/s - same-ish: difference falls within error
!nil?: 786999.4 i/s - same-ish: difference falls within error
JRUBY 9.0.5.0
Warming up --------------------------------------
current 64.479k i/100ms
double !! 89.755k i/100ms
!nil? 93.341k i/100ms
Calculating -------------------------------------
current 1.847M (± 5.0%) i/s - 9.220M in 5.005233s
double !! 1.753M (± 5.8%) i/s - 8.796M in 5.040223s
!nil? 1.769M (± 3.8%) i/s - 8.867M in 5.020606s
Comparison:
current: 1847004.7 i/s
!nil?: 1768749.9 i/s - same-ish: difference falls within error
double !!: 1752919.2 i/s - same-ish: difference falls within error
Ruby:
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin14]
Hardware:
Hardware Overview:
Model Name: MacBook Pro
Model Identifier: MacBookPro12,1
Processor Name: Intel Core i7
Processor Speed: 3.1 GHz
Number of Processors: 1
Total Number of Cores: 2
L2 Cache (per Core): 256 KB
L3 Cache: 4 MB
Memory: 16 GB
Boot ROM Version: MBP121.0167.B16
SMC Version (system): 2.28f7
Software:
System Software Overview:
System Version: OS X 10.11.4 (15E65)
Kernel Version: Darwin 15.4.0
Time since boot: 5:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment