Karim Belabas on Sun, 08 Jan 2023 23:48:04 +0100
|
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
- To: "Ruud H.G. van Tol" <rvtol@isolution.nl>
- Subject: Re: lookup-optimization
- From: Karim Belabas <Karim.Belabas@math.u-bordeaux.fr>
- Date: Sun, 8 Jan 2023 23:46:55 +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=1673218012; c=relaxed/relaxed; bh=qDEcAam+05CpM8qbp3mOl/ebwN9TfZBIw7qTLihg1+E=; 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=doxI/mB8Dlq4+P+Rrw7y/5qsV7p9Si83SYbVY6W0jkRxKWA8wMcQUe2tXpiQ3N9P5pew8ZXDOkvbV9CZV9CxPuSg2HUWh5YU6+qKy7zbKZbFmwEBFI7yovBohkc9vXClbXm2wnQvF7h3lQjDGR3fEPigF2HCjYLfC9t8bgR5FjySG8ERyW3SthpoXuC3D/sTx8wYHYC5NAzIdl56zT7NFSULAcW6yoCZLaX13yqzFYGyxmueTPiOXoEQzS7mici2xELuTl5kH8YN7VGs4SZwtskit+WofDShsI5lqH0+D5nmxS1OxqE4xFHhicXKN3mt2soW7gEJFU+LeI9mciVRJVKMf0vffvWkgGvMs6Cb9cG6196ib77QVWJ7vcAt9xyLslFJyrx6vdFkwcdz0gvpihJClvrKcCjXZBI6PbQnLYfJaGzCoHPqtUJsgKI4rCwi1KlsPggq9NkaQdt2L5d24H76i//HLa9JTz8r4hRZ+3jCTi1i3PzRpAU6wEWIE2FbP+XeAYyj5L4YLqK9oH0jcsO0Rm1QFnwkE66jNanwdqqTqz1wZO7GqphrykyfH9cTtiNPfCctK2wFhLvpqxnbr4qBLy3OeZvCRxzXT7zzTeQgejqRDp+vAN59g3zHssCgzYRCM/23CSsVgsBgwu232xEQLhZV8bN5sH4ootkyTbc=
- Arc-seal: i=1; a=rsa-sha256; d=math.u-bordeaux.fr; s=openarc; t=1673218012; cv=none; b=FAZo2PZl+rd3YVbChFOpAnnmxdl8VrzRWa7cY5QGgl4DGAOOf10JYki0fldn422PDj+c+BRaOn9HtTfVK3+doWRQ2pB+B2K7TfHwTzUUKECDh8bgA7fJTuqyEcJk0Q7SsSqP4Z+DfKFGiGixO0ISRzdYPv+10dYkfhLG1Gh/Rr9Q7u4GXIrT6QIJrSQP/yesDgeQ7oMfzVAOU0VGrQ2Pn2JPIy1bcmgV/vZquhMbXyyroSvE4Y5W+CzU7USM4lD4U9QHRcEjpVXie6YXqFjI2OFo/qVxwIWP5odXth44I3BEDhjGwLSWlz+sqa5G7OByV+tPjXWSVrMf8T88KrJ5m+BSZNf5S67/Qvfy7vLCitNbb/tVXVQiVuRkbcRyW13FR7LHhT01hNVFjVTRAQfpsqz7vfipB333SaTebbxcyYzQwTkIHccfHTNuGJ7N8ZZ8u68q698fx92juIO3GbWPrg3it0ar++BX/dpLIiVzT1F814pQuioIbazhIEQ4BGa6eLv9i/B6YYliHD4b/+PXdOqYSP3J4ECjYWXc5nd/bPvCtGcLshQbPtFxzvjeAq0ScbKrhe1QMSkl/LhZg7RHrwDrs87FdMccS1F+05LVW3xMMWRHJSs2C7ZXy2Bok9FZKeyTS3D8N9X0yN7Ahm1mCkgKKKU6X6IelnkO47Zz8lo=
- Authentication-results: smail; arc=none
- Cc: pari-users@pari.math.u-bordeaux.fr
- Delivery-date: Sun, 08 Jan 2023 23:48:04 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=math.u-bordeaux.fr; s=2022; t=1673218012; bh=qDEcAam+05CpM8qbp3mOl/ebwN9TfZBIw7qTLihg1+E=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=bQJs3CP3UmOD5ZYaI5YPgC7tw8D2ySLXPlL4Fhw2MpqLDjK90g8v26Uf1sSEioBXO hWPUxU5JDQAUoe+ekdvupgZ0U+NbCrjpaqvLO8BidgWYRD3CeKnUr8YwUdsfW0nDIB MjgBIede/3zLiMY0geJMNfAdD2X/VfjT34DQJDOkRzMR0BMaGFAf9gHLiFAxepfWKx erFyfhKo1Ix7RoHk8WuZMcz66OjV1pqWBODk5nGj8GvB6ZyS+gW6b748Gnw0eZiCfw /2sfihADzyJqMIOyENRnjvRNauOa/GP+PCwV6K6Nls1mGfHnP7pFYy1MTdZAtuV6s7 S21vZY4cyuXnkLHdnNz4Yypq41qCkHM6k7JQFQ0wU/0ucDcXvbBf1M5DT8/uUbLbY/ WP4xZ5bboSA6A4AfI1w8cMtRST3NeiwzcMhDopwsRnbWgxKdGmR1Y96cvQlUID7eRZ TR+vS7Fhzedf8n6xCa7bH51oiQWoy6bz2Jz19Pk8AiFTejFCLhMrL/TGdhcmqYnp29 o7X9L4E/cG6xQqVCcgUjzGubMgvMigEopqbDAHXVS9XMr8BROh7VO14bVAmQetLQeN StMy94TggBp8ANRq5JZoT6RXwKRGOFoH7Iq7FIdTHeap1/X1wz0eg1XdbK2+OwpFXZ wGZKJuBtony0U/gNedC8ROWI=
- In-reply-to: <3296bf3c-11ee-d3d5-89a8-9088af372b5d@isolution.nl>
- Mail-followup-to: "Ruud H.G. van Tol" <rvtol@isolution.nl>, pari-users@pari.math.u-bordeaux.fr
- References: <3296bf3c-11ee-d3d5-89a8-9088af372b5d@isolution.nl>
* Ruud H.G. van Tol [2023-01-08 21:48]:
>
> In the below code I build up a set 's',
> against which I first check each new case
> for "congruence".
>
> If no such case was found,
> then the new case gets added to the set.
>
> Each element of 's' is a tuple
> of a power-of-2 and an offset.
>
> (1) I wonder if Mod() is better for that. Or store the sum?
> (2) Would vecsearch() be good to use for this?
> (3) Any ideas on doing things differently, are welcome.
Nothing really new, I just streamlined the code to understand what it
was doing. I also
- removed the costly (useless) 'Set'
- avoided the computation of 2^p (check whether valuation at 2 >= p instead).
- used bit operations when possible
All this only gains a small constant factor, though. (Less than 2.)
If I were to sort the lookup set, I would sort wrt the ѕecond entry to
maximize the likelihood of an early abort in the 'valuation' loop.
(And use a dynamic data structure so as to directly insert the new
element in the already sorted list. Didn't bother.)
A243115(N = 575) =
{ my(r= [], s= []);
forstep(v2 = 3, N, 4,
my(found = 0);
for (i = 1, #s,
if (valuation(s[i][2] - v2, 2) >= s[i][1], found = 1; break));
if (found, next);
my(p2 = 0, v3 = v2);
until (v3 <= v2,
if (bitand(v3,1), v3 += v3 >> 1 + 1;
, v3 >>= 1);
p2++);
r = concat(r, v2);
s = concat(s, [ [p2, v2] ]));
r;
}
Before:
? A243115(10^5);
time = 3,824 ms.
? A243115(10^6);
time = 3min, 23,506 ms.
After:
? A243115(10^5);
time = 2,056 ms.
? A243115(10^6);
time = 2min, 689 ms.
Cheers,
K.B.
P.S. A slightly faster variant using an installed function:
install(vali,lG)
A243115(N = 575) =
{ my(r= [], s= []);
forstep(v2 = 3, N, 4,
my(found = 0);
for (i = 1, #s,
if (vali(s[i][2] - v2) >= s[i][1], found = 1; break));
if (found, next);
my(p2 = 0, v3 = v2);
until (v3 <= v2,
if (bitand(v3,1), v3 += v3 >> 1 + 1;
, v3 >>= 1);
p2++);
r = concat(r, v2);
s = concat(s, [ [p2, v2] ]));
r;
}
? A243115(10^5);
time = 1,856 ms.
? A243115(10^6);
time = 1min, 49,184 ms.
--
Pr 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/
`