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

49 lines, 28 significant
1#!/usr/bin/env python2
2"""
3fib_recursive.py
4"""
5from __future__ import print_function
6
7import os
8
9from mycpp.mylib import log
10
11
12def fib_recursive(n):
13 # type: (int) -> int
14 if n == 0:
15 return 1
16 if n == 1:
17 return 1
18 return fib_recursive(n - 1) + fib_recursive(n - 2)
19
20
21def run_tests():
22 # type: () -> None
23 x = 33
24
25 # NOTE: This is very slow and should be separated
26 result = fib_recursive(x)
27 log('fib_recursive(%d) = %d', x, result)
28
29
30def run_benchmarks():
31 # type: () -> None
32 n = 1 # Just one iteration is enough
33
34 x = 33
35 result = -1
36
37 i = 0
38 while i < n:
39 result = fib_recursive(x)
40 i += 1
41 log('fib_recursive(%d) = %d', x, result)
42
43
44if __name__ == '__main__':
45 if os.getenv('BENCHMARK'):
46 log('Benchmarking...')
47 run_benchmarks()
48 else:
49 run_tests()