Found that use of iterator which just extend field is cost additional computation time (about 20% time more)
nim
import std/[json, times, monotimes, tables]
let a = %*{"a": 1, "b": 2, "c": 3}
var start = getMonoTime()
for i in 1..1_000_000:
for k, v in a.fields.pairs:
discard
echo getMonoTime()-start
start = getMonoTime()
for i in 1..1_000_000:
for k, v in a.pairs:
discard
echo getMonoTime()-start
but i dont see such effect when trying to simplify case
iterator q1(): int =
for i in 1..1_000_000:
yield i
iterator q2(): int =
for i in q1():
yield i
var start = getMonoTime()
for i in q1():
discard
echo getMonoTime()-start
start = getMonoTime()
for i in q2():
discard
echo getMonoTime()-start
have managed to simplify
type A = object
a: int
b: int
iterator q1(): A =
for i in 1..1_000_000:
yield A(a: i)
iterator q2(): A =
for i in q1():
yield i
var b = 0
start = getMonoTime()
for i in q1():
b += i.a
echo getMonoTime()-start
start = getMonoTime()
for i in q2():
b += i.a
echo getMonoTime()-start
{
{
NI i;
NI res;
i = (NI)0;
res = ((NI)1);
{
while (1) {
if (!(res <= ((NI)1000000))) goto LA6;
i = ((NI) (res));
nimZeroMem((void*)(&i__test_u35), sizeof(tyObject_A__iB29a5WBVxBSpVcclUg0YVA));
i__test_u35.a = i;
b__test_u34 += i__test_u35.a;
res += ((NI)1);
} LA6: ;
}
}
}
{
{
tyObject_A__iB29a5WBVxBSpVcclUg0YVA i_2;
nimZeroMem((void*)(&i_2), sizeof(tyObject_A__iB29a5WBVxBSpVcclUg0YVA));
{
NI i_3;
NI res_2;
i_3 = (NI)0;
res_2 = ((NI)1);
{
while (1) {
if (!(res_2 <= ((NI)1000000))) goto LA15;
i_3 = ((NI) (res_2));
nimZeroMem((void*)(&i_2), sizeof(tyObject_A__iB29a5WBVxBSpVcclUg0YVA));
i_2.a = i_3;
i__test_u124 = i_2;
b__test_u34 += i__test_u124.a;
res_2 += ((NI)1);
} LA15: ;
}
}
}
}
Maybe there is a copy which causes slowness? I am not sure