OCaml Difference List
Dlist (Difference List) is a purely functional list-like data structure supporting O(1) concatenation. This is particularly useful for efficient list construction from many lists. Dlist is a lazy immutable data type with no side effect.
This data structure is very handy when your algorithm requires lots of concatenations from many lists.
opam install dlist
opam install ocp-build ounit core_bench ocp-build build ocp-build install
let dlst1 = Dlist.of_list [1;2;3;4;5] in let dlst2 = Dlist.of_list [6;7;8;9;10] in let dlst = Dlist.append dlst1 dlst2 in (* O(1) *) let lst = Dlist.to_list dlst
Generate 100 lists where every list contains 100 random integers, and concatenate them with
@ for list and
Dlist.append for Dlist.
Name Time/Run Speedup ------- ------------- --------- list 112_341_988 1.00 dlist 12_571_208 8.94