OILS / mycpp / examples / fib_iter.py View on Github | oilshell.org

53 lines, 33 significant
1#!/usr/bin/env python2
2"""
3fib.py: Simple Python 2 program to translate to C++.
4"""
5from __future__ import print_function
6
7import os
8
9from mycpp.mylib import log
10
11
12def 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
25def run_tests():
26 # type: () -> None
27 x = 33
28
29 result = fib_iter(x)
30 log('fib_iter(%d) = %d', x, result)
31
32
33def 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
48if __name__ == '__main__':
49 if os.getenv('BENCHMARK'):
50 log('Benchmarking...')
51 run_benchmarks()
52 else:
53 run_tests()