查看命令执行的时间
如果将%time放在输入行的开始,那么命令执行的时间会显示在命令的输出之后。例如,我们可以比较几种不同求幂运算的时间。下面的时间在不同的电脑和不同的Sage版本中可能很不一样。首先看原生Python的计算时间:
sage: %time a = int(1938)^int(99484)CPU times: user 0.66 s, sys: 0.00 s, total: 0.66 sWall time: 0.66
这表示总共用了0.66秒,”Wall time”即总时间是0.66秒。如果你的电脑还在运行很多其他的程序,那么wall time可能比CPU时间多很多。
下面我们查看用Sage的整数类型计算指数的时间,它是通过Cython调用GMP库实现的。
sage: %time a = 1938^99484CPU times: user 0.04 s, sys: 0.00 s, total: 0.04 sWall time: 0.04
使用PARI的C语言接口:
sage: %time a = pari(1938)^pari(99484)CPU times: user 0.05 s, sys: 0.00 s, total: 0.05 sWall time: 0.05
GMP稍微好一点(跟预期的一样,因为Sage内置的PARI也调用了GMP的整数运算)。
还可以象下面这样使用cputime命令来查看程序块的运行时间。
sage: t = cputime()sage: a = int(1938)^int(99484)sage: b = 1938^99484sage: c = pari(1938)^pari(99484)sage: cputime(t) # somewhat random output0.64
sage: cputime?...Return the time in CPU second sinceSagestarted, or with optionalargument t, return the time since time t.INPUT:t -- (optional) float, time in CPU secondsOUTPUT:float -- time in CPU seconds
walltime和cputime类似,只是它计算的是wall time.
我们还能计算Sage中包含的其他计算机代数系统的运算能力。 下面的例子中,每个系统我们都先执行一个无关紧要的命令来启动相应程序。最接近的时间是wall time。然而,如果wall time和CPU时间差的较多时,说明有可能存在性能方面的差异。
sage: time 1938^99484;CPU times: user 0.01 s, sys: 0.00 s, total: 0.01 sWall time: 0.01sage: gp(0)0sage: time g = gp('1938^99484')CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 sWall time: 0.04sage: maxima(0)0sage: time g = maxima('1938^99484')CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 sWall time: 0.30sage: kash(0)0sage: time g = kash('1938^99484')CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 sWall time: 0.04sage: mathematica(0)0sage: time g = mathematica('1938^99484')CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 sWall time: 0.03sage: maple(0)0sage: time g = maple('1938^99484')CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 sWall time: 0.11sage: gap(0)0sage: time g = gap.eval('1938^99484;;')CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 sWall time: 1.02
这个测试中,GAP和Maxima最慢(使用的是sage.math.washington.edu的主机)。 由于pexpect接口的开销,将这些与Sage进行比较可能是不公平的。
