I'm working with Haskell. Standard list concatenation is naive and slow. My situation is I have an algorithm that builds up a single list concatenating (order doesn't matter, so it could be either prepend or append or a combination) many times, and then returns it. The result will then be used only once. High performance is critical.
So, it's a pretty simple situation. I heard about difference lists and that it helps with this situation. But is that the best option?
The lists could grow to be large: 100,000's of entries.
It means that the content of the List are fixed or constant after declaration, that is, they are read-only. If any attempt made to add, delete and update elements in the List, UnsupportedOperationException is thrown. An ImmutableList does not allow null element either.
ImmutableList can be created by various methods. These include: In Java, use of () with Set, Map or List to create an Immutable List. Please Note: The programs below are of Java 9. Hence you would need a Java 9 compiler to run them. In Guava, ImmnutableList class provides a function Builder ().
If any attempt made to add, delete and update elements in the List, UnsupportedOperationException is thrown. An ImmutableList does not allow null element either. If any attempt made to create an ImmutableList with null element, NullPointerException is thrown.
This is an empirical question and should be answered empirically. Reasonable alternatives include
Standard list with cons (called "prepend" in your question)
Difference list (John Hughes list) with constant-time append
Algebraic data type supporting constant-time append:
data Alist a = ANil | ASingle a | AAppend (Alist a) (Alist a)
List of lists with final concat
.
All these will take linear time. But constant factors matter, and the only way to find out is to build and measure. If you want you can create a microbenchmark that is completely faithful to your original code, but performs only list operations, by logging every list operation into a writer monad. But that is probably a huge pain in the ass and just not worth it. Instead, write a simple benchmark, compile (with optimization turned on), and measure.
And please let us know the results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With