It’s been bothering me for a while now that I don’t know the most efficient way to append/concatenate strings in Haskell. I thought that this might be an interesting motivation for learning how to use Criterion.
Here’s the first version of the function I want to test:
This is pretty standard stuff: I want to build a string in left-to-right order. It uses the append operator,
Here’s another variant using
And another using
And, finally, using sequence which supports
O(1) append to either end of the sequence:
Here’s the whole program:
This depends on the following non-
Here’s a full, working project if you like.
I run it using
stack as follows:
stack clean stack build stack exec -- concat-vs-append --output bench.html
You can view the output from the program here.
ShowS, it really makes no difference. Don’t use
Seq. I will need to analyse further to make sure that this is a meaningful test. See you later!
All content © 2018 Richard Cook. All rights reserved.