Comparing performance between IPython and SageMath

232 views
Skip to first unread message

Christophe Bal

unread,
Jan 24, 2015, 5:41:00 AM1/24/15
to sage-...@googlegroups.com
Hello.

Here are two worksheets almost identical.
  1. https://cloud.sagemath.com/projects/bcf4f485-52db-49db-ac8d-07e1b9b39795/files/IFS.ipynb
  2. https://cloud.sagemath.com/projects/bcf4f485-52db-49db-ac8d-07e1b9b39795/files/IFS.sagews
One uses Sage and the other IPython. The difference of performance is important. Is there a real "normal" reason for that ? I am not complaining but just a little disappointed.


Christophe BAL
Enseignant de mathématiques en Lycée et développeur Python amateur
---
French math teacher in a "Lycée" and Python amateur developer

Nathan Dunfield

unread,
Jan 24, 2015, 11:41:44 AM1/24/15
to sage-...@googlegroups.com
Here are two worksheets almost identical.
  1. https://cloud.sagemath.com/projects/bcf4f485-52db-49db-ac8d-07e1b9b39795/files/IFS.ipynb
  2. https://cloud.sagemath.com/projects/bcf4f485-52db-49db-ac8d-07e1b9b39795/files/IFS.sagews
One uses Sage and the other IPython. The difference of performance is important. Is there a real "normal" reason for that ? 

Technically, it has to do with how Sage's graphics interface is built on top of matplotlib.  However, you can *also* use matplotlib directly in Sage, and that should be just as fast in an IPython notebook as in a Sage Math Cloud one.  Personally, anymore I just use "raw" matplotlib in Sage for this and other reasons (finer control of various things, tons of examples in the matplotlib docs, etc.).

Best,

Nathan

William Stein

unread,
Jan 24, 2015, 2:15:40 PM1/24/15
to sage-cloud
On Sat, Jan 24, 2015 at 8:41 AM, Nathan Dunfield
<nathan.mich...@gmail.com> wrote:
>> Here are two worksheets almost identical.
>>
>>
>> https://cloud.sagemath.com/projects/bcf4f485-52db-49db-ac8d-07e1b9b39795/files/IFS.ipynb
>>

If you want to share an IPython notebook publicly, it's much better to
click on the "Publish" button (upper right) and share the html link it
generates. If you share the ipynb file people have to copy it to
their own project before they can look at it. I did that for you
and here is the link:

https://cloud.sagemath.com/projects/bcf4f485-52db-49db-ac8d-07e1b9b39795/files/IFS.html

It's very relevant to the correct description of why IPython displays
an image quickly and how the two worksheets are actually very
different (see below).

>> https://cloud.sagemath.com/projects/bcf4f485-52db-49db-ac8d-07e1b9b39795/files/IFS.sagews
>>
>> One uses Sage and the other IPython. The difference of performance is
>> important. Is there a real "normal" reason for that ?
>
>
> Technically, it has to do with how Sage's graphics interface is built on top
> of matplotlib.

I don't agree...

> However, you can *also* use matplotlib directly in Sage, and
> that should be just as fast in an IPython notebook as in a Sage Math Cloud
> one. Personally, anymore I just use "raw" matplotlib in Sage for this and
> other reasons (finer control of various things, tons of examples in the
> matplotlib docs, etc.).

It won't make any difference.

The difference in this case is that in IPython notebook by default
plotting returns a *very* low-resolution base64-encoded png image,
whereas Sage worksheets return an infinite resolution vector graphics
SVG image, which is much, much more complicated. To make the Sage
version be drawn and displayed more quickly, do

show(plot(points(zip(x_pts, y_pts)), '.'), svg=False)

In most cases SVG looks much, much better than the graphics produced
by default by IPython notebook.

Note that IPython notebook embeds all plots (by default) in the
underlying JSON document of the notebook, so drawing a few plots can
make the notebook file very large. So it's important for them to
ensure the size is small. In constrast, SMC stores all images in an
external database, keyed by the sha1 hash of the content.

The image in https://cloud.sagemath.com/projects/bcf4f485-52db-49db-ac8d-07e1b9b39795/files/IFS.sagews
doesn't appear right now because the database deleted the image a
little before the file was saved; I've made the auto-image-deletion
from the database way, way too aggressive, and am changing that in a
moment.

I played around with this worksheet when writing this:

https://cloud.sagemath.com/projects/4a5f0542-5873-4eed-a85c-a18c706e8bcd/files/support/2015-01-24-performance.sagews

William

>
> Best,
>
> Nathan
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-cloud" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-cloud+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sage-cloud/00e68a0a-aef5-4b5d-9be2-0b07c6d87ba1%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org

Nathan Dunfield

unread,
Jan 24, 2015, 2:37:41 PM1/24/15
to sage-...@googlegroups.com
> Technically, it has to do with how Sage's graphics interface is built on top
> of matplotlib.

I don't agree...

You're correct, of course.  I was thinking of the fact that syntax such as 

sage: sum([point((x,y)) for x, y in zip(xs, ys)], Graphics())

is much slower than calling "scatter_plot" or using matplotlib primitives directly.   I presume that's because the above code is creating a zillion Graphics objects along way, but regardless this wasn't what Christophe was doing. 

Sorry for the confusion, 

Nathan

William Stein

unread,
Jan 24, 2015, 2:47:32 PM1/24/15
to sage-cloud
Your point is of course also very relevant -- it's just not the main
term in this particular case. I wish I would have emphasized that
more in my response above. I was lacking of tact, having just woke
up...

>
> Nathan
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-cloud" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-cloud+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sage-cloud/d90845de-3d16-483a-90f5-0b87b6854b89%40googlegroups.com.

Harald Schilly

unread,
Jan 24, 2015, 4:24:11 PM1/24/15
to sage-...@googlegroups.com
On Sat, Jan 24, 2015 at 8:14 PM, William Stein <wst...@gmail.com> wrote:
> To make the Sage
> version be drawn and displayed more quickly, do
>
> show(plot(points(zip(x_pts, y_pts)), '.'), svg=False)


Just for the sake of testing the other way around, you can also tell
the IPython notebook's "%matplotlib inline" to always generate SVGs. I
find that particularly useful when you transform them to PDFs via a
LaTeX nbconvert export.

In your IPython profile configuration (create that file if it doesn't exist):

~/.ipython/profile_default/ipython_config.py

add those two lines:

c = get_config()
c.InlineBackend.figure_format = 'svg'

-- Harald

Christophe Bal

unread,
Jan 25, 2015, 11:23:39 AM1/25/15
to sage-...@googlegroups.com
Thanks a lot for the very clear explanations.

Christophe BAL

Heikki Arponen

unread,
Jan 26, 2015, 2:26:10 PM1/26/15
to sage-...@googlegroups.com


On Saturday, January 24, 2015 at 11:24:11 PM UTC+2, Harald Schilly wrote:
Just for the sake of testing the other way around, you can also tell
the IPython notebook's "%matplotlib inline" to always generate SVGs. I
find that particularly useful when you transform them to PDFs via a
LaTeX nbconvert export.

Hey wow that's cool... need to try that out right away! I'm sick of the  ipynb png images...
Reply all
Reply to author
Forward
0 new messages