PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/test3/timing2.py

http://gmpy.googlecode.com/
Python | 29 lines | 23 code | 6 blank | 0 comment | 5 complexity | 0841648c6179b7526b8fb0cb7986e91f 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 0, 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