Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use @+ instead of dup for duplicating strings
This is faster and a bit cheaper on the memory side as well. require "bundler/inline" gemfile(true) do source "https://rubygems.org" gem "benchmark-ips" gem "benchmark-memory" end Benchmark.ips do |x| x.report("dup") { "foo".dup } x.report("@+") { +"foo" } x.compare! end Benchmark.memory do |x| x.report("dup") { "foo".dup } x.report("@+") { +"foo" } x.compare! end Warming up -------------------------------------- dup 642.978k i/100ms @+ 1.350M i/100ms Calculating ------------------------------------- dup 6.646M (± 0.6%) i/s - 33.435M in 5.030737s @+ 13.503M (± 0.7%) i/s - 67.515M in 5.000186s Comparison: @+: 13503099.2 i/s dup: 6646345.1 i/s - 2.03x slower Calculating ------------------------------------- dup 80.000 memsize ( 0.000 retained) 2.000 objects ( 0.000 retained) 1.000 strings ( 0.000 retained) @+ 40.000 memsize ( 0.000 retained) 1.000 objects ( 0.000 retained) 1.000 strings ( 0.000 retained) Comparison: @+: 40 allocated dup: 80 allocated - 2.00x more
- Loading branch information