add softfloat's license
[sfpy.git] / timing.py
1 """Some basic performance tests for the wrapper class"""
2
3 import timeit
4
5
6 def ops_sec(stmt, setup):
7 # print('----')
8 # print(setup)
9 print('-- timing: --')
10 print(stmt)
11 print('----')
12
13 timer = timeit.Timer(stmt, setup)
14 iters, time = timer.autorange()
15
16 rate = iters/time
17 if rate > 1000000000:
18 erate = rate / 1000000000
19 unit = 'Gops'
20 elif rate > 1000000:
21 erate = rate / 1000000
22 unit = 'Mops'
23 elif rate > 1000:
24 erate = rate / 1000
25 unit = 'kops'
26 else:
27 erate = rate
28 unit = 'ops'
29
30 print('{} ops, {} s, {} {}/s\n'.format(iters, time, erate, unit))
31
32
33 setup = """import sfpy
34 import SoftPosit.python.softposit as sp1
35 import SoftPosit.python._softposit as sp2
36 zero = 0.0
37 one = 1.0
38 sfpy_a = sfpy.Posit16(1.3)
39 sfpy_b = sfpy.Posit16(-0.5)
40 sfpy_c = sfpy.Posit16(15.0)
41 sfpy_q = sfpy.Quire16(0)
42
43 sp1_a = sp1.posit16(1.3)
44 sp1_b = sp1.posit16(-0.5)
45 sp1_c = sp1.posit16(15.0)
46 sp1_q = sp1.quire16()
47 """
48 """
49 sp2_a = sp2.convertDoubleToP16(1.3)
50 sp2_b = sp2.convertDoubleToP16(-0.5)
51 sp2_c = sp2.convertDoubleToP16(15.0)
52 sp2_q = sp2.new_quire16_t()
53 """
54
55 ops_sec('pass', setup)
56 ops_sec('sfpy.Posit16(0)', setup)
57 ops_sec('sp1.posit16(0)', setup)
58 #ops_sec('sp2.new_posit16_t()', setup)
59
60 ops_sec('one + zero', setup)
61 ops_sec('sfpy_a + sfpy_b', setup)
62 ops_sec('sp1_a + sp1_b', setup)
63 #ops_sec('sp2_a + sp2_b', setup)
64
65 ops_sec('sfpy_c += sfpy_b', setup)
66
67 # ops_sec('P8(0)', setup)
68 # ops_sec('P8("0")', setup)
69 # ops_sec('P8.from_bits(0)', setup)
70
71 # ops_sec('1.1 + 2', setup)
72 # ops_sec('a + b', setup)
73 # ops_sec('a.add(b)', setup)
74 # ops_sec('zero += 1', setup)
75 # ops_sec('a += b', setup)
76 # ops_sec('a.iadd(b)', setup)
77
78 # ops_sec('q.fdp_add(b, c)', setup)