[Issue 10024] New: product function
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri May 3 16:41:42 PDT 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10024
Summary: product function
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: Phobos
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2013-05-03 16:41:41 PDT ---
I suggest to add a product() function to Phobos, similar to sum().
This benchnarking code shows an algorithm better than the naive one when it's
used on bigints:
import std.stdio, std.bigint, std.algorithm, std.random;
T product1(T)(T[] seq) {
typeof(return) result = 1;
foreach (s; seq)
result *= s;
return result;
}
T product2(T)(T[] seq) { // faster
if (seq.length < 50) {
typeof(return) result = 1;
foreach (s; seq)
result *= s;
return result;
} else {
immutable mid = seq.length / 2;
return product2(seq[0 .. mid]) * product2(seq[mid .. $]);
}
}
void main() {
BigInt[2 ^^ 16] items;
foreach (i, ref b; items)
b = i + 1;
items[].randomShuffle;
//auto r = items.product1;
auto r = items.product2;
//r.writeln;
}
What about T == InputRange?
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list