fast-crystal
fast-crystal copied to clipboard
Strings concatenation performance
I'm afraid, concatenation of strings is a disputable question at the moment. For example, here are my results:
require "benchmark"
puts Crystal::DESCRIPTION
FOO = "foo"
BAR = "bar"
def method_1
FOO + BAR
end
def method_2
"#{FOO}#{BAR}"
end
Benchmark.ips do |bm|
bm.report "method_1" { method_1 }
bm.report "method_2" { method_2 }
end
method_1 58.65M ( 17.05ns) (± 0.73%) 32 B/op fastest
method_2 11.84M ( 84.47ns) (± 1.14%) 208 B/op 4.95× slower
The benchmark was built with options --release --no-debug
, if it matters.
Also, there is an issue on the topic.
Crystal 0.27.2 [60760a546] (2019-02-05) LLVM: 4.0.0 Default target: x86_64-unknown-linux-gnu
That's something that @asterite mentioned in this commit comment : https://github.com/konung/fast-crystal/commit/cbc2ff616257beadaae7aecf40b09fa477ac0d9d#r32484649
I'll have a chance to go over some of the other commits and issues over the weekend and see how it changes. Also something I've noticed: some benchmarks have very different results on Ubuntu vs Mac.
Specifically, there is a benchmark with Enumerable#count ( I haven't added it yet to repo) that shows 2x difference between two OSs
some benchmarks have very different results on Ubuntu vs Mac
Oh, I see, that's a misfortune. The only thing that I may suggest here, is to persuade potential visitors to question everything and test the cases with their real OS.
But the cases you provide are really great to see the alternatives, thank you for that.
Yep. No problem. I'll be adding more as I think of them.
1 option I'm pondering is to automate this a bit with Docker - so that there is more of a common env for benchmarks. But I will add a warning that OS & version of LLVM may affect the final results, pretty wildly. I mean in most cases - 95% - it should be pretty much the same, but I've have seen where it varies.
For this me this was more of en exercise to learn more about Crystal, when it comes to specific app performance - it may vary wildly based on many other things, so all benchmarks come with a bit of inherent caveat.
I;ll leave this open until I update the Readme.