Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The kernel slows execution of code by about factor of 2 #27

Open
edgarcosta opened this issue Jul 11, 2022 · 2 comments
Open

The kernel slows execution of code by about factor of 2 #27

edgarcosta opened this issue Jul 11, 2022 · 2 comments

Comments

@edgarcosta
Copy link
Member

Try the code

sieve(n) =
{
    my(S,p); /* necessary to avoid creating global S,p */
    S = apply((x)->1,[1..n]);
    for(p=2,floor(sqrt(n)),
        if(S[p],forstep(m=p+p,n,p,S[m]=0));
    );
    return(select((x)->S[x],[2..n]));
};
c = gettime();
w = getwalltime();
print(sieve(10^6)==select((p)->isprime(p),[1..10^6]));
print("Time: ", strtime(gettime() - c));
print("Time: ", strtime(getwalltime() - w));
c = gettime();
w = getwalltime();
print(length(sieve(10^7)));
print("Time: ", strtime(gettime() - c));
print("Time: ", strtime(getwalltime() - w));

in the kernel and in the terminal.
In the terminal I obtain:

Time: 2,535 ms
Time: 2,644 ms
  *** apply: Warning: increasing stack size to 1024000000.
  *** select: Warning: increasing stack size to 2048000000.
664579
Time: 31,440 ms
Time: 32,261 ms

and in the Kernel

Time: 4,479 ms
Time: 5,030 ms
  ***   Warning: increasing stack size to 1024000000.
  *** select: Warning: increasing stack size to 1200001024.
664579
Time: 51,229 ms
Time: 52,096 ms

any insight on how to fix this?

My alternative is to have a kernel based on pexpect, but then I can't easily support completion

@videlec
Copy link
Collaborator

videlec commented Dec 8, 2022

Easier test case: quadclassunit(1 - 2^110) and quadclassunit(1 - 2^120) (take respectively ~3sec and ~15sec).

  • I confirm the x2 slowdown compared to GP
  • It turns out that execution with cypari2 inside IPython shows the same x2 slowdown... but does not show up in CPython

There is a intriguing difference of behaviour if we load sage or not inside CPython

>>> from cypari2 import Pari
>>> from time import time
>>> pari  =Pari()
>>> pari.allocatemem()
PARI stack size set to 16007168 bytes, maximum size set to 16007168
>>> t0 = time(); s = pari("quadclassunit(1 - 2^110)"); print(time() - t0)
2.941565990447998

Get twice slower if we load sage

$ python
Python 3.10.8 (main, Nov  1 2022, 14:18:21) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sage.all import *
>>> from time import time
>>> from cypari2 import Pari
>>> pari = Pari()
>>> t0 = time(); s = pari("quadclassunit(1 - 2^110)"); print(time() - t0)
5.750366926193237

Since the PARIKernel inherits from ipykernel.kernelbase.Kernel it seems plausible that the problem is related to IPython.

@edgarcosta
Copy link
Member Author

edgarcosta commented Dec 8, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants