Karim Belabas on Sat, 26 Nov 2022 09:31:07 +0100
|
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
- To: "Ruud H.G. van Tol" <rvtol@isolution.nl>
- Subject: Re: 3^k performance
- From: Karim Belabas <Karim.Belabas@math.u-bordeaux.fr>
- Date: Sat, 26 Nov 2022 09:29:58 +0100
- Arc-authentication-results: i=1; smail; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; d=math.u-bordeaux.fr; s=openarc; t=1669451400; c=relaxed/relaxed; bh=CR+Uh9HK5ENExe1l0GR06etBtf8Gtg/mbzPAtnNVOjg=; h=DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: Mail-Followup-To:References:MIME-Version:Content-Type: Content-Disposition:Content-Transfer-Encoding:In-Reply-To; b=wnKNeSxcvl2f4H3Rdmp5QL+SSsM/C8A9fBVGMBjc/pMrHILo4gIGwzshOb/UU/CXBTim1oFqAPMa8cr0o6627bIPhq32Sqgk1iIH3ub/JIupA82QwNi0fXZzgY3vxTbItW1vwNPN7Lr3gKzuvmfd9kXCg+sLAE4nz3o3xaNQHpWnuDIcFjOnjhSG0syYWLIlrFJiMOf4MxFwC9Y+BS05TWseRlwnYSMICn6nLvn2sIrz6D73AtLIHmzfrgyDeHRAqfDsJc3kQtm7es0Kqlxpp5o5eflWr9QAAaW3euxxJ5tl/YIlBb67dY2p3NiHvyw9ongFBqLaK9HKDPEvlCl4MulNKJd0lrKy5CHdWsGe48ecrgNfndyABk9ic4cPyYD1ttC7/JXqYl+08kTdFrgt7PaZBpyBa/7bZv+bN4XkNlHwu+Wv4OvcyutRYPSEAobMRllD/Md55A41p3zZPcYDYahnziDYIz9HkjnU3gwh08K3SQRdMY0iyklRSMRsDQo+I7a//+9RHE3a0k50AFr1ncMh9ynKD2A4CZr5mRtpc0YTtpxs4XvsnvBD33665uce132nzHRMXc8PFuQSpiOhrP1HnW50H9ZLXcmzQIiAp4lcvxl/I0gteSUcw3MhpY38OxqXSCiDydtIqZnMdUnAwpn2x+FJEcdbfaCj67aFwHs=
- Arc-seal: i=1; a=rsa-sha256; d=math.u-bordeaux.fr; s=openarc; t=1669451400; cv=none; b=bu52ZOARMb8I6bzg8OjCktqI7Fp9L7m2244wdqpu6zBwMOMlVyntbeeh6QnZjNKIYo4KJFwWF0v9TGvQ5nhK7W8M60vf1DexPBneaqWGDMIdegPZJPgidOqXnI3mIGhgV5JXuAsUXvVW0gQxXK+1Z0AQCWwJqPweqAnZ8H2z5gfm1NbXGA3g0Fc0NrEFMOD4+zVqlDn1Sb8L9JKVX3GYpaZ+92uPqWHHW/sN6gNIiYQMkC9PssXCh/mIOJ3UXi1slTyIjYKV4lJXaxa/X4GZhslyrR7FDI+f+t0bX4AdPk0Co0lvqxZUG4Lc3b17epmpIINolr99mESuQCA0CTbOkVjlR5UgApqovWKt4TUfi02Zh9TL3hLG0qyyq18l7thHhWbzYzvt2qsnPftx4+tsa/rNKP0n0RWRXNyWeW981t8+s01uPfXYHJbpdHcG9EQAh8lvLlL3ASMAefwWmTuu2lPxkIRtU4Q6iTa06fGoQtmk8CXshrcZovQc6qzx98muIL2wf7l0e5ezud6QVBpc1RQsp0IrjakKPJ7WXtClgq6jLn6AI5271WKfkiThPBBhAicDMy9a2cSpOb4dDLQbce1XSGGUwabEbXX463bRX6xTf8F+7Vgj23kJLRHcHXiEvL2UmaOcBHT//4YpUS2XR21K7nZ+SGmhuxkpScYgM+I=
- Authentication-results: smail; dmarc=none header.from=math.u-bordeaux.fr
- Authentication-results: smail; arc=none
- Cc: pari-dev@pari.math.u-bordeaux.fr
- Delivery-date: Sat, 26 Nov 2022 09:31:07 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=math.u-bordeaux.fr; s=2022; t=1669451400; bh=CR+Uh9HK5ENExe1l0GR06etBtf8Gtg/mbzPAtnNVOjg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ftWf2PF5JtTAX5GryomNA4SfyJyafPJDj/VVHHMtKfrcZU1nTZXqfs7TBC6Z/QxDg 2TqhxaJ6Q0Rz/HNoeXHfMvTtS3gz+CKhEvNixq6lURfgNIahV3B1ui0H7oy62ELjCM 3lPuVt/VV/fWqpAJRyYqYJtEt29onEN85jE7hzcGSmuxvcRUazqhr1g64Bm4m4UpdF qlXAgwndHl7/orW7tNQJeXakpYGAsbKPeO/gmKTuJ6C4Z5oxhzpaamDwHxVNRYD5aA iKGRCTKtGfGeqvxxc9u3jjWmqUrvhH8c9uwundSLtJ6LU6DMxN2dS5rfgo1muR/jc6 KKQdUmUkxeHP+85gRtlDElPgk+JJajKb9qHGot62OvljASFEg5zuSuktLd+14IMuAg RiuJOCBQtu0lW15wRpNnA2oyIj56yA+GtyiqS2oFFRs1zsSDEuzY+3v+KoTIQTjpni Ko9gzZQN7fylGWysdBgGiPiKhzpCteMxP/NscwTzGf1MUt9OfbhRBRwfLpR/dTgrcM 1yHHlUPIFBnMvPRUPmekGverQjdXzdoP6qC3weWg/Bu2GtoH/Q+GSmQHW5SWGW5Ce/ Ba8SeQu71DdlQf6QmZmyoN/vLCQBSDJYSSJ7+GnuNUqDZh+BvEP1KrB3GpM/xUYyTY zU3aaM9kNDRXJHYqcVQ4FUKQ=
- In-reply-to: <de8f200c-386e-3028-f44b-519b9ce445c1@isolution.nl>
- Mail-followup-to: "Ruud H.G. van Tol" <rvtol@isolution.nl>, pari-dev@pari.math.u-bordeaux.fr
- References: <de8f200c-386e-3028-f44b-519b9ce445c1@isolution.nl>
* Ruud H.G. van Tol [2022-11-26 08:47]:
>
> A022330_1(n)=1+n+sum(k=1,n,logint(3^k,2))
>
> A022330_2(n)=my(t=1);1+n+sum(k=1,n,logint(t*=3,2))
>
> ? A022330_1(10^5)
> cpu time = 8,322 ms, real time = 8,352 ms.
> %15 = 7924941755
>
> ? A022330_2(10^5)
> cpu time = 215 ms, real time = 217 ms.
> %16 = 7924941755
>
> So about a 40x difference.
>
> Is that worthwhile to look into?
This is expected:
(09:02) gp > for(k=1,10^5,3^k)
time = 8,409 ms.
(09:02) gp > my(t=1);for(k=1,10^5,t*=3)
time = 175 ms.
Assume x is a small integer (here x = 3).
Computing x^k as x * t where t = x^(k-1) is already known involves a
single multiplication by x, which is done in linear tine wrt the size of t,
i.e. about s = k * log2(x).
Computing x^k from scratch involves essentially log2(k) consecutive
squarings of an increasingly large integer [ and also a few
multiplications by x whose cost is negligible ]. The final squaring
only, namely (x^r)^2 where r = k >> 1, has a cost which is superlinear
in the size of x^r, i.e about s / 2. Concretely :
(09:18) gp > t = 3^(10^5 >> 1);
(09:18) gp > for(i=1,10^4,t^2)
time = 1,805 ms.
(09:19) gp > T = t^2;
(09:19) gp > for(i=1,10^4,T^2)
time = 4,726 ms.
So doubling the size of t multiplies the squaring time by about 2.62 in
this range, which would correspond to a complexity M(s) ~ s^1.39
(where 1.39 ~ log2(2.62)) for the cost of a multiplication of size s.
Now compare s and (s / 2)^1.39 for s = log2(3^10^5), the actual size
you're interested in:
(09:20) gp > s = log(3^10^5)/log(2);
(09:20) gp > (s/2)^1.39 / s
%3 = 40.698247890865989130716669156266722278
So a 40 x difference is quite expected !
> How to best approach that?
Never compute from scratch when previous results can be reused :-)
Cheers,
K.B.
--
Karim Belabas / U. Bordeaux, vice-président en charge du Numérique
Institut de Mathématiques de Bordeaux UMR 5251 - (+33) 05 40 00 29 77
http://www.math.u-bordeaux.fr/~kbelabas/
`