ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 24 Feb 2021 18:51:24 +0100Python kernel seriously slower than SageMath kernelhttps://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/Hi, I just found the Python kernel is seriously slower than the SageMath one.
This was the snippet to reproduce:
# intersection profiling with Python session
from sage.all import *
def run(num):
cube = (polytopes.cube() * 37 / 45).change_ring(QQ)
for i in range(num):
hspace = Polyhedron(ieqs=[[1/2, 1/(i+1), 0, 0]]).change_ring(QQ)
intersection = hspace.intersection(cube)
%timeit run(100)
The timing for SageMath kernel is `1 loop, best of 5: 186 ms per loop`, while that for Python is `1 loop, best of 5: 7.07 s per loop`. With this snippet I think no variable was cached. I also cprofiled a stand-alone .py file with the same lines (expect the `%timeit` line), whose timing is close to the Python kernel. Any idea what could cause this huge performance difference?
[Image which kernel to use](https://ibb.co/DYg6f7F)Tue, 23 Feb 2021 23:40:29 +0100https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/Comment by Sébastien for <p>Hi, I just found the Python kernel is seriously slower than the SageMath one.</p>
<p>This was the snippet to reproduce:</p>
<pre><code># intersection profiling with Python session
from sage.all import *
def run(num):
cube = (polytopes.cube() * 37 / 45).change_ring(QQ)
for i in range(num):
hspace = Polyhedron(ieqs=[[1/2, 1/(i+1), 0, 0]]).change_ring(QQ)
intersection = hspace.intersection(cube)
%timeit run(100)
</code></pre>
<p>The timing for SageMath kernel is <code>1 loop, best of 5: 186 ms per loop</code>, while that for Python is <code>1 loop, best of 5: 7.07 s per loop</code>. With this snippet I think no variable was cached. I also cprofiled a stand-alone .py file with the same lines (expect the <code>%timeit</code> line), whose timing is close to the Python kernel. Any idea what could cause this huge performance difference?</p>
<p><a href="https://ibb.co/DYg6f7F">Image which kernel to use</a></p>
https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55867#post-id-55867Are you comparing SageNB running on Python2 with Sage Jupyter running on Python 3 ? If yes, can you redo the comparison with SageMath 9.1 Jupyter running with Python 2? To be sure that it is not because of the passage Python 2 to Python 3. But indeed, John's hypothesis about RDF vs QQ seems to be the best explanation.Wed, 24 Feb 2021 09:24:37 +0100https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55867#post-id-55867Comment by zhaiyu for <p>Hi, I just found the Python kernel is seriously slower than the SageMath one.</p>
<p>This was the snippet to reproduce:</p>
<pre><code># intersection profiling with Python session
from sage.all import *
def run(num):
cube = (polytopes.cube() * 37 / 45).change_ring(QQ)
for i in range(num):
hspace = Polyhedron(ieqs=[[1/2, 1/(i+1), 0, 0]]).change_ring(QQ)
intersection = hspace.intersection(cube)
%timeit run(100)
</code></pre>
<p>The timing for SageMath kernel is <code>1 loop, best of 5: 186 ms per loop</code>, while that for Python is <code>1 loop, best of 5: 7.07 s per loop</code>. With this snippet I think no variable was cached. I also cprofiled a stand-alone .py file with the same lines (expect the <code>%timeit</code> line), whose timing is close to the Python kernel. Any idea what could cause this huge performance difference?</p>
<p><a href="https://ibb.co/DYg6f7F">Image which kernel to use</a></p>
https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55870#post-id-55870Sorry for the confusion: I actually mean the comparison between the SageMath 9.1 kernel and the Python 3 kernel, both of which running in Jupyter. Question edited.Wed, 24 Feb 2021 10:25:51 +0100https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55870#post-id-55870Comment by John Palmieri for <p>Hi, I just found the Python kernel is seriously slower than the SageMath one.</p>
<p>This was the snippet to reproduce:</p>
<pre><code># intersection profiling with Python session
from sage.all import *
def run(num):
cube = (polytopes.cube() * 37 / 45).change_ring(QQ)
for i in range(num):
hspace = Polyhedron(ieqs=[[1/2, 1/(i+1), 0, 0]]).change_ring(QQ)
intersection = hspace.intersection(cube)
%timeit run(100)
</code></pre>
<p>The timing for SageMath kernel is <code>1 loop, best of 5: 186 ms per loop</code>, while that for Python is <code>1 loop, best of 5: 7.07 s per loop</code>. With this snippet I think no variable was cached. I also cprofiled a stand-alone .py file with the same lines (expect the <code>%timeit</code> line), whose timing is close to the Python kernel. Any idea what could cause this huge performance difference?</p>
<p><a href="https://ibb.co/DYg6f7F">Image which kernel to use</a></p>
https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55854#post-id-55854I don't think it's reasonable to compare a Python Jupyter notebook to SageNB: you should instead compare a SageMath Jupyter notebook to SageNB, in which case I don't see any significant time difference. (I do see a difference with a Python Jupyter notebook.)Wed, 24 Feb 2021 01:35:09 +0100https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55854#post-id-55854Comment by John Palmieri for <p>Hi, I just found the Python kernel is seriously slower than the SageMath one.</p>
<p>This was the snippet to reproduce:</p>
<pre><code># intersection profiling with Python session
from sage.all import *
def run(num):
cube = (polytopes.cube() * 37 / 45).change_ring(QQ)
for i in range(num):
hspace = Polyhedron(ieqs=[[1/2, 1/(i+1), 0, 0]]).change_ring(QQ)
intersection = hspace.intersection(cube)
%timeit run(100)
</code></pre>
<p>The timing for SageMath kernel is <code>1 loop, best of 5: 186 ms per loop</code>, while that for Python is <code>1 loop, best of 5: 7.07 s per loop</code>. With this snippet I think no variable was cached. I also cprofiled a stand-alone .py file with the same lines (expect the <code>%timeit</code> line), whose timing is close to the Python kernel. Any idea what could cause this huge performance difference?</p>
<p><a href="https://ibb.co/DYg6f7F">Image which kernel to use</a></p>
https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55856#post-id-55856For example, `polytopes.cube() * 37/45` returns "A 3-dimensional polyhedron in RDF^3 ..." in a Python notebook while it returns "A 3-dimensional polyhedron in QQ^3 ..." in a Sage notebook.Wed, 24 Feb 2021 01:40:20 +0100https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55856#post-id-55856Comment by zhaiyu for <p>Hi, I just found the Python kernel is seriously slower than the SageMath one.</p>
<p>This was the snippet to reproduce:</p>
<pre><code># intersection profiling with Python session
from sage.all import *
def run(num):
cube = (polytopes.cube() * 37 / 45).change_ring(QQ)
for i in range(num):
hspace = Polyhedron(ieqs=[[1/2, 1/(i+1), 0, 0]]).change_ring(QQ)
intersection = hspace.intersection(cube)
%timeit run(100)
</code></pre>
<p>The timing for SageMath kernel is <code>1 loop, best of 5: 186 ms per loop</code>, while that for Python is <code>1 loop, best of 5: 7.07 s per loop</code>. With this snippet I think no variable was cached. I also cprofiled a stand-alone .py file with the same lines (expect the <code>%timeit</code> line), whose timing is close to the Python kernel. Any idea what could cause this huge performance difference?</p>
<p><a href="https://ibb.co/DYg6f7F">Image which kernel to use</a></p>
https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55871#post-id-55871I think John made a good point. It's not fair to compare Python Jupyter notebook to [SageMath Jupyter notebook or SageNB]. For performance issue I would stick with a 'native' Sage runtime. But does this mean faster execution or better integration (i.e. import Sage as a library in a Python script) we can only pick one?Wed, 24 Feb 2021 10:40:38 +0100https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55871#post-id-55871Comment by zhaiyu for <p>Hi, I just found the Python kernel is seriously slower than the SageMath one.</p>
<p>This was the snippet to reproduce:</p>
<pre><code># intersection profiling with Python session
from sage.all import *
def run(num):
cube = (polytopes.cube() * 37 / 45).change_ring(QQ)
for i in range(num):
hspace = Polyhedron(ieqs=[[1/2, 1/(i+1), 0, 0]]).change_ring(QQ)
intersection = hspace.intersection(cube)
%timeit run(100)
</code></pre>
<p>The timing for SageMath kernel is <code>1 loop, best of 5: 186 ms per loop</code>, while that for Python is <code>1 loop, best of 5: 7.07 s per loop</code>. With this snippet I think no variable was cached. I also cprofiled a stand-alone .py file with the same lines (expect the <code>%timeit</code> line), whose timing is close to the Python kernel. Any idea what could cause this huge performance difference?</p>
<p><a href="https://ibb.co/DYg6f7F">Image which kernel to use</a></p>
https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55852#post-id-55852My env:
macOS 10.15.6 (RAM 16GB)
SageMath 9.1Tue, 23 Feb 2021 23:42:31 +0100https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55852#post-id-55852Comment by John Palmieri for <p>Hi, I just found the Python kernel is seriously slower than the SageMath one.</p>
<p>This was the snippet to reproduce:</p>
<pre><code># intersection profiling with Python session
from sage.all import *
def run(num):
cube = (polytopes.cube() * 37 / 45).change_ring(QQ)
for i in range(num):
hspace = Polyhedron(ieqs=[[1/2, 1/(i+1), 0, 0]]).change_ring(QQ)
intersection = hspace.intersection(cube)
%timeit run(100)
</code></pre>
<p>The timing for SageMath kernel is <code>1 loop, best of 5: 186 ms per loop</code>, while that for Python is <code>1 loop, best of 5: 7.07 s per loop</code>. With this snippet I think no variable was cached. I also cprofiled a stand-alone .py file with the same lines (expect the <code>%timeit</code> line), whose timing is close to the Python kernel. Any idea what could cause this huge performance difference?</p>
<p><a href="https://ibb.co/DYg6f7F">Image which kernel to use</a></p>
https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55855#post-id-55855Maybe one difference is that in a Sage notebook (SageNB or Jupyter), the numbers are treated as Sage integers, rationals, etc., while there is going to be a lot of coercion and conversion when you're dealing with a Python notebook. Just a guess.Wed, 24 Feb 2021 01:36:28 +0100https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55855#post-id-55855Comment by Emmanuel Charpentier for <p>Hi, I just found the Python kernel is seriously slower than the SageMath one.</p>
<p>This was the snippet to reproduce:</p>
<pre><code># intersection profiling with Python session
from sage.all import *
def run(num):
cube = (polytopes.cube() * 37 / 45).change_ring(QQ)
for i in range(num):
hspace = Polyhedron(ieqs=[[1/2, 1/(i+1), 0, 0]]).change_ring(QQ)
intersection = hspace.intersection(cube)
%timeit run(100)
</code></pre>
<p>The timing for SageMath kernel is <code>1 loop, best of 5: 186 ms per loop</code>, while that for Python is <code>1 loop, best of 5: 7.07 s per loop</code>. With this snippet I think no variable was cached. I also cprofiled a stand-alone .py file with the same lines (expect the <code>%timeit</code> line), whose timing is close to the Python kernel. Any idea what could cause this huge performance difference?</p>
<p><a href="https://ibb.co/DYg6f7F">Image which kernel to use</a></p>
https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55853#post-id-55853Can't reproduce : On my machine, I need abut 88 ms per loop in command line or through `sage-shell-mode` in `emacs`, and a bit less than 100 ms per loop in Jupyter (marginal and probably irrelevant difference...).Wed, 24 Feb 2021 00:25:09 +0100https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55853#post-id-55853Comment by John Palmieri for <p>Hi, I just found the Python kernel is seriously slower than the SageMath one.</p>
<p>This was the snippet to reproduce:</p>
<pre><code># intersection profiling with Python session
from sage.all import *
def run(num):
cube = (polytopes.cube() * 37 / 45).change_ring(QQ)
for i in range(num):
hspace = Polyhedron(ieqs=[[1/2, 1/(i+1), 0, 0]]).change_ring(QQ)
intersection = hspace.intersection(cube)
%timeit run(100)
</code></pre>
<p>The timing for SageMath kernel is <code>1 loop, best of 5: 186 ms per loop</code>, while that for Python is <code>1 loop, best of 5: 7.07 s per loop</code>. With this snippet I think no variable was cached. I also cprofiled a stand-alone .py file with the same lines (expect the <code>%timeit</code> line), whose timing is close to the Python kernel. Any idea what could cause this huge performance difference?</p>
<p><a href="https://ibb.co/DYg6f7F">Image which kernel to use</a></p>
https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55892#post-id-55892If you import Sage as a library in a Python script, you have to be careful with how you use numbers. For example in plain Python, 3/4 is a float while in Sage it is preparsed and is a Sage rational number. Much of the Sage library is written in plain Python so it is not hard to do, but you need to be intentional about whether you want "3" to be a Python int or a Sage Integer, etc.Wed, 24 Feb 2021 18:48:22 +0100https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55892#post-id-55892Comment by John Palmieri for <p>Hi, I just found the Python kernel is seriously slower than the SageMath one.</p>
<p>This was the snippet to reproduce:</p>
<pre><code># intersection profiling with Python session
from sage.all import *
def run(num):
cube = (polytopes.cube() * 37 / 45).change_ring(QQ)
for i in range(num):
hspace = Polyhedron(ieqs=[[1/2, 1/(i+1), 0, 0]]).change_ring(QQ)
intersection = hspace.intersection(cube)
%timeit run(100)
</code></pre>
<p>The timing for SageMath kernel is <code>1 loop, best of 5: 186 ms per loop</code>, while that for Python is <code>1 loop, best of 5: 7.07 s per loop</code>. With this snippet I think no variable was cached. I also cprofiled a stand-alone .py file with the same lines (expect the <code>%timeit</code> line), whose timing is close to the Python kernel. Any idea what could cause this huge performance difference?</p>
<p><a href="https://ibb.co/DYg6f7F">Image which kernel to use</a></p>
https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55894#post-id-55894You can speed up your `run` function, for example, by making some changes like replacing `37` with `Integer(37)`.Wed, 24 Feb 2021 18:51:24 +0100https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?comment=55894#post-id-55894Answer by zhaiyu for <p>Hi, I just found the Python kernel is seriously slower than the SageMath one.</p>
<p>This was the snippet to reproduce:</p>
<pre><code># intersection profiling with Python session
from sage.all import *
def run(num):
cube = (polytopes.cube() * 37 / 45).change_ring(QQ)
for i in range(num):
hspace = Polyhedron(ieqs=[[1/2, 1/(i+1), 0, 0]]).change_ring(QQ)
intersection = hspace.intersection(cube)
%timeit run(100)
</code></pre>
<p>The timing for SageMath kernel is <code>1 loop, best of 5: 186 ms per loop</code>, while that for Python is <code>1 loop, best of 5: 7.07 s per loop</code>. With this snippet I think no variable was cached. I also cprofiled a stand-alone .py file with the same lines (expect the <code>%timeit</code> line), whose timing is close to the Python kernel. Any idea what could cause this huge performance difference?</p>
<p><a href="https://ibb.co/DYg6f7F">Image which kernel to use</a></p>
https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?answer=55887#post-id-55887Noted from John's comments, I can confirm the difference stems from the coercion and conversion stuff.
Given `script.py` and `script.sage` (same contents):
from sage.all import *
import time
def run(num):
cube = (polytopes.cube() * 37 / 45).change_ring(QQ)
for i in range(num):
hspace = Polyhedron(ieqs=[[(i+1)/(i+2), 1/(i+1), (i+2), (i+53)]]).change_ring(QQ)
intersection = hspace.intersection(cube)
tik = time.time()
run(100)
print('time elapsed: {}'.format(time.time() - tik), 's')
With `python script.py`, I got `time elapsed: 8.750308990478516 s`.
With `sage script.sage`, I got `time elapsed: 0.6618611812591553 s`, whose execution automatically generated a python file `script.sage.py` which looks like this:
# This file was *autogenerated* from the file profile.sage
from sage.all_cmdline import * # import sage library
_sage_const_37 = Integer(37); _sage_const_45 = Integer(45); _sage_const_1 = Integer(1); _sage_const_2 = $
from sage.all import *
import time
def run(num):
cube = (polytopes.cube() * _sage_const_37 / _sage_const_45 ).change_ring(QQ)
for i in range(num):
hspace = Polyhedron(ieqs=[[(i+_sage_const_1 )/(i+_sage_const_2 ), _sage_const_1 /(i+_sage_const_$
intersection = hspace.intersection(cube)
tik = time.time()
run(_sage_const_100 )
print('time elapsed: {}'.format(time.time() - tik), 's')
I then ran `python script.sage.py` and got `time elapsed: 0.6441857814788818 s`.
This conversion is documented in [the tutorial](https://doc.sagemath.org/html/en/tutorial/programming.html#creating-compiled-code):
> When Sage loads example.sage it
> converts it to Python, which is then
> executed by the Python interpreter.
> This conversion is minimal; it mainly
> involves wrapping integer literals in
> Integer() floating point literals in
> RealNumber(), replacing ^’s by **’s,
> and replacing e.g., R.2 by R.gen(2).
> The converted version of example.sage
> is contained in the same directory as
> example.sage and is called
> example.sage.py.Wed, 24 Feb 2021 18:07:13 +0100https://ask.sagemath.org/question/55851/python-kernel-seriously-slower-than-sagemath-kernel/?answer=55887#post-id-55887