File: timing2.py

package info (click to toggle)
python-gmpy 1.15-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 768 kB
  • ctags: 739
  • sloc: ansic: 9,767; python: 5,627; makefile: 38
file content (28 lines) | stat: -rw-r--r-- 699 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import gmpy, time

def timedfib(n, zero):
    start=time.clock()
    a=zero; b=a+1
    for i in range(n):
        a,b=b,a+b
    stend=time.clock()
    return type(zero), stend-start, a

def timedfibsp(n, one):
    start=time.clock()
    result=gmpy.fib(n)
    stend=time.clock()
    return type(one), stend-start, result

def test(n=100*1000):
    print "%dth Fibonacci number of various types:" % n
    for z in 0L, gmpy.mpz(0), gmpy.mpf(0):
        tip, tim, tot = timedfib(n, z)
        print "    %5.3f %s %s" % (tim, gmpy.fdigits(tot,10,6), tip)
    tip, tim, tot = timedfibsp(n, 1)
    print "    %5.3f %s %s" % (tim, gmpy.fdigits(tot,10,6), "gmpy.fib")


if __name__=='__main__':
    test()