Last active
October 22, 2015 10:54
-
-
Save Bodacious/f73e7661cf58eedcb635 to your computer and use it in GitHub Desktop.
Most efficient way of returning a constant string from a Ruby method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Rehearsal ------------------------------------------------- | |
| with freez 1.490000 0.010000 1.500000 ( 1.493176) | |
| with constant 1.500000 0.000000 1.500000 ( 1.499597) | |
| with new 2.060000 0.000000 2.060000 ( 2.064578) | |
| ---------------------------------------- total: 5.060000sec | |
| user system total real | |
| with freez 1.470000 0.000000 1.470000 ( 1.480206) | |
| with constant 1.500000 0.000000 1.500000 ( 1.510912) | |
| with new 2.030000 0.000000 2.030000 ( 2.038113) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "benchmark" | |
| Benchmark.bmbm do |test| | |
| class MyClass | |
| FREEZ = "abcdefg".freeze | |
| CONST = "abcdefg" | |
| def with_const | |
| CONST | |
| end | |
| def with_strin | |
| "abcdefg" | |
| end | |
| def with_freez | |
| FREEZ | |
| end | |
| end | |
| TIMES = 10000000 | |
| test.report "with constant" do | |
| TIMES.times do | |
| MyClass.new.with_const | |
| end | |
| end | |
| test.report "with new" do | |
| TIMES.times do | |
| MyClass.new.with_strin | |
| end | |
| end | |
| test.report "with freez" do | |
| TIMES.times do | |
| MyClass.new.with_freez | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment