Skip to content

Instantly share code, notes, and snippets.

@huacnlee
Created September 30, 2014 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huacnlee/014292aee825adad76b8 to your computer and use it in GitHub Desktop.
Save huacnlee/014292aee825adad76b8 to your computer and use it in GitHub Desktop.
String freeze vs no freeze
task :test_string_freeze_vs_nofreeze do
str = "this is a example string"
Benchmark.ips do |x|
x.report("no_freeze") {
str.index("example")
}
x.report("freeze") {
str.index("example".freeze)
}
x.compare!
end
end
@huacnlee
Copy link
Author

Calculating -------------------------------------
           no_freeze      1751 i/100ms
              freeze      1697 i/100ms
-------------------------------------------------
           no_freeze  3074454.3 (±10.8%) i/s -    9220766 in   3.133668s
              freeze  3944374.9 (±8.6%) i/s -   13747397 in   3.518218s

Comparison:
              freeze:  3944374.9 i/s
           no_freeze:  3074454.3 i/s - 1.28x slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment