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, ++
.
concat
Here’s another variant using concat
:
ShowS
And another using showString
from ShowS
:
Seq
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-base
packages:
containers
criterion
random-strings
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.
Use ++
, concat
or 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!
Content © 2024 Richard Cook. All rights reserved.