1 | #!/usr/bin/env python2
|
2 | """
|
3 | fib.py: Simple Python 2 program to translate to C++.
|
4 | """
|
5 | from __future__ import print_function
|
6 |
|
7 | import os
|
8 |
|
9 | from mycpp.mylib import log
|
10 |
|
11 |
|
12 | def fib_iter(n):
|
13 | # type: (int) -> int
|
14 | a = 0
|
15 | b = 1
|
16 | i = 0
|
17 | while i < n:
|
18 | tmp = a + b
|
19 | a = b
|
20 | b = tmp
|
21 | i += 1
|
22 | return b
|
23 |
|
24 |
|
25 | def run_tests():
|
26 | # type: () -> None
|
27 | x = 33
|
28 |
|
29 | result = fib_iter(x)
|
30 | log('fib_iter(%d) = %d', x, result)
|
31 |
|
32 |
|
33 | def run_benchmarks():
|
34 | # type: () -> None
|
35 | n = 500000
|
36 |
|
37 | x = 33
|
38 | result = -1
|
39 |
|
40 | i = 0
|
41 | while i < n:
|
42 | result = fib_iter(x)
|
43 | i += 1
|
44 | log('fib_iter(%d) = %d', x, result)
|
45 | log('Ran %d iterations of fib_iter', n)
|
46 |
|
47 |
|
48 | if __name__ == '__main__':
|
49 | if os.getenv('BENCHMARK'):
|
50 | log('Benchmarking...')
|
51 | run_benchmarks()
|
52 | else:
|
53 | run_tests()
|