Reduce has dreadful performance?
Russel Winder via Digitalmars-d
digitalmars-d at puremagic.com
Thu Jun 18 03:27:30 PDT 2015
On a given machine, the code:
double sequential_loop(const int n, const double delta) {
auto sum = 0.0;
foreach (immutable i; 1 .. n + 1) {
immutable x = (i - 0.5) * delta;
sum += 1.0 / (1.0 + x * x);
}
return 4.0 * delta * sum;
}
runs in about 6.70s. However the code:
double sequential_reduce(const int n, const double delta) {
return 4.0 * delta * reduce!((double t, int i){immutable x = (i -
0.5) * delta; return t + 1.0 / (1.0 + x * x);})(0.0, iota(1, n + 1));
}
runs in about 17.03s, whilst:
double sequential_reduce_alt(const int n, const double delta) {
return 4.0 * delta * reduce!"a + b"(
map!((int i){immutable x = (i - 0.5) * delta; return 1.0 /
(1.0 + x * x);})(iota(1, n + 1)));
}
takes about 28.02s. Unless I am missing something (very possible), this
is not going to create a good advert for D as an imperative language
with declarative (internal iteration) expression.
--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder at ekiga.net
41 Buckmaster Road m: +44 7770 465 077 xmpp: russel at winder.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20150618/8b2c9871/attachment.sig>
More information about the Digitalmars-d
mailing list