Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Noted 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 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:

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.

Noted 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:

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.