Dude about ~ array concatenation performance
ddcovery
antoniocabreraperez at gmail.com
Tue Dec 1 22:49:55 UTC 2020
Yesterday I really shocked when, comparing one algorithm written
in javascript and the equivalent in D, javascript performed
better!!!
The idea is to translate the "3 lines sort" in haskell to
Javascript and D (with the limitations of each language). This
is not a quick sort test, but a "expressiveness" example of
functional orientation: you "declare" that the sorted version of
an array is, given one of it's elements, the sorted version of
the smaller ones, plus the item, plus the sorted version of the
greater ones.
In javascript
const sorted = ([pivot, ...others]) => pivot === void 0 ? [] : [
...sorted(others.filter(v => v < pivot)),
pivot,
...sorted(others.filter(v => v >= pivot))
];
In D
T[] sorted(T)(T[] values)
{
return values.length == 0 ? [] :
sorted(values[1 .. $].filter!(v => v < values[0]).array()) ~
items[0 .. 1] ~
sorted(values[1 .. $].filter!(v => v >= values[0]).array());
}
With 1 million Double numbers (generated randomly):
Javascript (node 12): 1507 ms
DMD: 2166 ms
With 6 million Double numbers
Javascript (node 12): 10776 ms
DMD: 15243 ms
You can find more detains in
https://github.com/ddcovery/expressive_sort
I will really appreciate some improvements... the only "rule" is
that "sorted" must be written, preferably, as a single
expression ("declarative" way, avoiding imperative instructions)
and, of course, you can't use the native library "sort" methods
:-) ).
More information about the Digitalmars-d-learn
mailing list