One Danger of Freedom Patches
I posted a benchmark on twitter about comparing a DateTime with a string. This is a short blurrrggghhh post about the benchmark and why there is such a performance discrepancy.
Here is the benchmark:
require 'benchmark/ips' require 'active_support/all' if ENV['AS'] require 'date' now = DateTime.now Benchmark.ips do |x| x.report("lhs") { now == "foo" } x.report("rhs") { "foo" == now } end First we’ll run the benchmark without Active Support, then we’ll run the benchmark with Active Support.
The Benchmarks Without Active Support [aaron@higgins rails (master)]$ bundle exec ruby argh.rb Calculating ------------------------------------- lhs 57389 i/100ms rhs 76222 i/100ms ------------------------------------------------- lhs 2020064.6 (±14.7%) i/s - 9870908 in 5.015172s rhs 3066573.4 (±13.2%) i/s - 15091956 in 5.012879s [aaron@higgins rails (master)]$ With Active Support [aaron@higgins rails (master)]$ AS=1 bundle exec ruby argh.rb Calculating ------------------------------------- lhs 4786 i/100ms rhs 26327 i/100ms ------------------------------------------------- lhs 62858.4 (±23.6%) i/s - 296732 in 5.019005s rhs 2866546.6 (±26.6%) i/s - 13031865 in 4.996482s [aaron@higgins rails (master)]$ Numbers! In the benchmarks without Active Support, the performance is fairly close. The standard deviation is pretty big, but the numbers are within the ballpark of each other.
Tenderlove Making