rails/rails — pull request #17310 by pabloh (2014-10-18 08:41:35+00:00)
Title: Optimize TimeWithZoneTest#strftime

Replace TimeWithZoneTest#strftime, with a version that doesn't create unnecessary String instances and avoid unnecessary method calls when they aren't needed.

Some benchmarks:
```ruby
#Worse scenario, every formating option present (from TimeWithZone) in use: 73% faster
Benchmark.ips do |bm|
bm.report('original') { @twz.strftime_original('%Y-%m-%d %H:%M:%S %Z %z %:z %::z') }
bm.report('optimized') { @twz.strftime('%Y-%m-%d %H:%M:%S %Z %z %:z %::z') }
end

#Calculating -------------------------------------
# original 2331 i/100ms
# optimized 3990 i/100ms
#-------------------------------------------------
# original 24536.5 (±1.1%) i/s - 123543 in 5.035705s
# optimized 42606.0 (±1.2%) i/s - 215460 in 5.057748s

#With 2 formating options: 2.9 times faster
Benchmark.ips do |bm|
bm.report('original') { @twz.strftime_original('%Y-%m-%d %H:%M:%S %Z %z') }
bm.report('optimized') { @twz.strftime('%Y-%m-%d %H:%M:%S %Z %z') }
end

#Calculating -------------------------------------
# original 2598 i/100ms
# optimized 6959 i/100ms
#-------------------------------------------------
# original 27054.3 (±1.0%) i/s - 137694 in 5.090060s
# optimized 79036.4 (±1.4%) i/s - 396663 in 5.019817s

#With 1 formating options: 5.5 times faster
Benchmark.ips do |bm|
bm.report('original') { @twz.strftime_original('%Y-%m-%d %H:%M:%S %Z') }
bm.report('optimized') { @twz.strftime('%Y-%m-%d %H:%M:%S %Z') }
end

#Calculating -------------------------------------
# original 2742 i/100ms
# optimized 12840 i/100ms
#-------------------------------------------------
# original 29109.9 (±1.4%) i/s - 148068 in 5.087531s
# optimized 160027.8 (±1.1%) i/s - 808920 in 5.055439s

#With no formating options: 8.1 times faster
Benchmark.ips do |bm|
bm.report('original') { @twz.strftime_original('%Y-%m-%d %H:%M:%S') }
bm.report('optimized') { @twz.strftime('%Y-%m-%d %H:%M:%S') }
end

#Calculating -------------------------------------
# original 2974 i/100ms
# optimized 19257 i/100ms
#-------------------------------------------------
# original 31817.4 (±1.0%) i/s - 160596 in 5.047983s
# optimized 258589.2 (±3.2%) i/s - 1309476 in 5.070831s
```