On Monday, 16 May 2016 at 23:01:44 UTC, ag0aep6g wrote:
On 05/17/2016 12:53 AM, Alex wrote:
Just as the reality (in my head) is: you can count something without
having written the natural numbers before you start to count...

iota does that, too. A iota struct doesn't store all the numbers it will emit. Just like a slice, a iota struct stores two numbers: the first and the amount of numbers. Iterating over it means counting, not reading pre-generated numbers from a list.

Especially, I don't have to create some strange structs containing just
a number, as I expect to have some millions of them.

Some million slices will take just as much space as some million iota structs. Storing a slice isn't free. If you create the slices on the fly, you can do that with iota too. No need to store them beforehand.

the space is the same, yes...
Added this to my test:

import std.conv : to;
import std.datetime;
size_t aM = 5; size_t bM = 80;
void f1() {auto io = iota(aM,bM);}
void f2() {auto sl = testFunc(arr, aM ,bM);}
auto r = benchmark!(f1, f2)(100_000);
auto f0Result = to!Duration(r[0]); // time f1 took to run 10,000 times auto f1Result = to!Duration(r[1]); // time f2 took to run 10,000 times
writeln("f1: ", f0Result);
writeln("f2: ", f1Result);

with dmd test44.d -release
the results are:
f1: 692 μs and 7 hnsecs
f2: 379 μs and 1 hnsec

Reply via email to