Two chunks but No allocation
Salih Dincer
salihdb at hotmail.com
Thu Mar 28 03:54:05 UTC 2024
On Wednesday, 27 March 2024 at 20:50:05 UTC, rkompass wrote:
> This works:
I decided to give the full code. Maybe then it will be better
understood what I mean. I actually pointed out the indirect
solution above but it's a bit ugly and I'm sure there must be a
better way?
```d
import std.datetime.stopwatch;
import std.stdio, std.algorithm,
std.range;
auto cube(T)(T n)
=> n * n * n;
auto S1(T, R)(R a, T n)
=> n.cube - n; //
auto s1(T)(T first, size_t skip = 3)
=> recurrence!S1(0, first).drop(skip);
enum ITERATIONS = 14_000;
enum BENCHMARKS = 100;
void main()
{
real result;
long total_time = 0;
for(int i = 0; i < BENCHMARKS; i++)
{
auto sw = StopWatch(AutoStart.no);
sw.start();
result = s1(3.0).take(ITERATIONS)
.chunks(2)
.map!(r => 4/r.front)
.array // <- can't it be done without this?
.chunks(2)
.map!"a[0] - a[1]"
.fold!"a + b"(3.0);
sw.stop();
total_time += sw.peek.total!"nsecs";
}
writefln("%f (nsecs)", total_time / BENCHMARKS / 1e9);
writefln("%0.16f (result)", result);```
} /*
0.000456 (nsecs)
3.1415926535890670 (result)
//*/
```
SDB at 79
More information about the Digitalmars-d-learn
mailing list