PageRenderTime 282ms CodeModel.GetById 80ms app.highlight 17ms RepoModel.GetById 74ms app.codeStats 1ms

/test2/timing2.py

http://gmpy.googlecode.com/
Python | 29 lines | 23 code | 6 blank | 0 comment | 5 complexity | 5d73751127ba308967dcd2b9cda13b00 MD5 | raw file
 1import time
 2import gmpy2 as gmpy
 3
 4def timedfib(n, zero):
 5    start=time.clock()
 6    a=zero; b=a+1
 7    for i in range(n):
 8        a,b=b,a+b
 9    stend=time.clock()
10    return type(zero), stend-start, a
11
12def timedfibsp(n, one):
13    start=time.clock()
14    result=gmpy.fib(n)
15    stend=time.clock()
16    return type(one), stend-start, result
17
18def test(n=100*1000):
19    print "%dth Fibonacci number of various types:" % n
20    for z in 0L, gmpy.mpz(0), gmpy.mpf(0):
21        tip, tim, tot = timedfib(n, z)
22        print "    %5.3f %s %s" % (tim, gmpy.fdigits(tot,10,6), tip)
23    tip, tim, tot = timedfibsp(n, 1)
24    print "    %5.3f %s %s" % (tim, gmpy.fdigits(tot,10,6), "gmpy.fib")
25
26
27if __name__=='__main__':
28    test()
29