Improve Haskell brainfuck2 benchmarks #166
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The existing
bf.hs
is slow and unidiomatic as it uses an immutable array to represent the tape, which takesO(n)
time to update. One of the most idiomatic ways to represent a brainfuck tape immutably in Haskell is with a zipper along an infinite stream, as that gives youO(1)
time complexity for all brainfuck operations. So this is what I changebf.hs
to use.If you are willing to use mutability then the optimal way to represent a brainfuck tape is an unboxed mutable vector with an immutable vector to represent the instructions. So I created
bf-vector.hs
which does this. I didn't deletebf-marray.hs
but that would be a pretty reasonable thing to add to this PR, as vectors are generally faster than arrays and the vector instruction representation is also faster.Performance/memory usage on my machine (3.1 GHz Intel Core i7, macOS 10.13.3, GHC 8.2.2):
These files are still most likely not optimal, as I didn't put much effort into optimizing them besides doing the idiomatic mutable thing and the idiomatic immutable thing plus just a couple more obvious strictness annotations. I would absolutely welcome further attempts to optimize these.