PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/test2/timing1.py

http://gmpy.googlecode.com/
Python | 50 lines | 41 code | 9 blank | 0 comment | 13 complexity | e50e2a07a48221103cb35b1954a510aa MD5 | raw file
 1import time
 2import gmpy2 as gmpy
 3
 4
 5try: sum
 6except NameError:
 7  def sum(x, z):
 8    for item in x: z += item
 9    return z
10
11def timedsum(n, zero):
12    start=time.clock()
13    tot=zero
14    for i in range(n):
15        tot+=i
16    stend=time.clock()
17    return type(zero), stend-start, tot
18
19def timedsum1(n, zero):
20    start=time.clock()
21    tot=sum(range(n), zero)
22    stend=time.clock()
23    return type(zero), stend-start, tot
24
25def timedmul(n, one):
26    start=time.clock()
27    tot=one
28    for i in range(n):
29        tot*=(i+1)
30    stend=time.clock()
31    return type(one), stend-start, tot
32
33def test(n=100*1000):
34    print "Sum of %d items of various types:" % n
35    for z in 0L, 0.0, gmpy.mpz(0), gmpy.mpf(0):
36        tip, tim, tot = timedsum(n, z)
37        print "    %5.3f %.0f %s" % (tim, float(tot), tip)
38    print "Sum of %d items of various types w/2.3 sum builtin:" % n
39    for z in 0L, 0.0, gmpy.mpz(0), gmpy.mpf(0):
40        tip, tim, tot = timedsum1(n, z)
41        print "    %5.3f %.0f %s" % (tim, float(tot), tip)
42    print "Mul of %d items of various types:" % (n//5)
43    for z in 1L, 1.0, gmpy.mpz(1), gmpy.mpf(1):
44        tip, tim, tot = timedmul(n//5, z)
45        print "    %5.3f %s" % (tim, tip)
46
47
48if __name__=='__main__':
49    test()
50