Bill Allombert on Wed, 28 Dec 2022 20:19:09 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: A022921-code


On Wed, Dec 28, 2022 at 07:53:49PM +0100, Ruud H.G. van Tol wrote:
> 
> On 2022-12-28 17:51, Karim Belabas wrote:
> > * Ruud H.G. van Tol [2022-12-28 16:28]:
> > > [ logint(3^(n+1),2) - logint(3^n,2) |n<-[0..98]]
> > > 
> > > is half as fast as
> > > 
> > > my(n0=logint(3^0,2)); [ -n0 + n0=logint(3^(n+1),2) |n<-[0..98]]
> > > 
> > > but IMO looks ugly.
> > > 
> > > Is there a both "nice" and fast way to do similar?
> > Not sure about "nice"; but faster, definitely:
> > 
> > ? N = 30000;
> > ? w0 = [logint(3^(n+1),2) - logint(3^n,2) | n<-[0..N]];
> > time = 767 ms.
> > 
> > ? my(n0=logint(3^0,2)); w1 = [-n0 + n0=logint(3^(n+1),2) |n<-[0..N]];
> > time = 355 ms.
> > 
> > ? my(v = [logint(x,2) | x <-powers(3, N+1)]); w = vector(#v-1, i, v[i+1]-v[i]);
> > time = 20 ms.
> > 
> > ? [w == w0, w == w1]
> > %4 = [1, 1]
> 
> Pretty "nice" to me.
> 
> 
> I'm testing on an M1-Max, and could *not* run your fastest alternative under
> 120ms.
> 
> 
> But then I slightly changed it to
> 
> my(v = [logint(x,2) | x <-powers(3, N+1, 1)]); w = vector(#v-1, i, v[i+1]-v[i])
> 
> (so added the explicit '1' with powers)
> 
> and then it would run at least twice as fast, and would (sometimes) finish in 29ms.
> How weird is that? (No need to answer!)

It is not:
CHANGES
  15- powers(t_INT, ...) was very slow

This will be fixed in 2.15.2 which should be released soon.

Cheers,
Bill.