| Bill Allombert on Sat, 25 Apr 2026 14:30:13 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
| Re: mapisdefined(m,k,&v) vs. iferr(mapget(m,k),E,) |
On Sat, Apr 25, 2026 at 01:52:46PM +0200, Ruud H.G. van Tol wrote:
>
> v=0; mapisdefined(m, k, &v); mapput(~m, k, v+1)
> vs.
> mapput(~m, k, iferr( mapget(m, k), E, 0)+1)
>
>
> I recently changed some code to use the iferr() variant, to make it run
> significantly faster.
This is faster because mapget only fail 100 out of 10^5 that is 0.1% of the time.
In fact you could do
{
my(nn=10^5,w=100,m=Map());
for(j=0,99,mapput(~m,j,[]));
for(i=1,nn,
my(k=random(w));
mapput(~m,k,concat(mapget(m,k),i))
);#m;
}
to avoid the iferr.
but the real slowdown is due to concat, you should use lists and mappapply:
{
my(nn=10^5,w=100,m=Map());
for(i=1,nn,
my(k=random(w));
mapapply(~m,k,(~y)->listput(~y,i),()->List(i))
);m;
}
(about 10 time faster)
Cheers,
Bill