Skip to content

Instantly share code, notes, and snippets.

View mgaskill's full-sized avatar

Michael Gaskill mgaskill

  • Las Vegas, NV
  • 15:50 (UTC -07:00)
View GitHub Profile
@mgaskill
mgaskill / gist:4577a6caea7a100f780b56f7a8364e6b
Last active May 17, 2016 21:44
Results from benchmark of Ruby nil checks
ruby 2.2.5p319 (2016-04-26 revision 54774) [x86_64-darwin14]
============================================================
Running tests for obj = 1000000 (Fixnum)
user system total real
obj: 0.970000 0.000000 0.970000 ( 0.987204)
nil_obj: 0.980000 0.010000 0.990000 ( 0.980796)
obj.nil?: 1.250000 0.000000 1.250000 ( 1.268564)
nil_obj.nil?: 1.280000 0.000000 1.280000 ( 1.287800)
!obj: 1.050000 0.000000 1.050000 ( 1.064061)
!nil_obj: 1.070000 0.000000 1.070000 ( 1.170393)
@mgaskill
mgaskill / benchmark-formula.rb
Last active May 23, 2016 05:41
Ruby benchmark program for a simple two-operand, one operator expression parser
require 'benchmark'
def match_gaskill(formula)
return [] unless (match = formula.match(/^\s*(-?\d+(?:\.\d+)?)\s*([+\/*-])\s*(-?\d+(?:\.\d+)?)\s*$/))
return [match[1].to_f, match[2], match[3].to_f]
end
def match_sweaver(formula)
return [] unless (match = formula.match(/(?<operand1>(?:\d+(?:\.\d+)?)|(?:\.\d+))\s*(?<operator>[+\/*-])\s*(?<operand2>(?:\d+(?:\.\d+)?)|(?:\.\d+))/))
return [match[1].to_f, match[2], match[3].to_f]