Files
graphene/Examples/python-simple/scripts/fibonacci.py
Paweł Marczewski 1d25612006 [CI] Enable pylint unconditionally, fix violations
Pylint output was filtered so that many files with existing pylint
violations were allowed to stay broken.

I made sure all files pass pylint, but whitelisted some rules that
we commonly disable:

* missing docstrings: most of the code is tests/internal anyway
* invalid-name: too many violations, and we commonly use one- or
  two-character names (like "a, b" or "t1, t2") which is
  disallowed by this rule; we could tweak it and then fix
  remaining violations such as camel-case or lowercase constants
* fixme: we leave TODOs as a matter of practice, same as in C
* high-level style rules like too-few-* and too-many-*,
  no-self-use

Hopefully that will make using pylint less annoying, while also
catching serious issues (such as unused variables or imports).
2020-11-17 13:45:09 -08:00

56 lines
1.3 KiB
Python

"""
Downloaded from https://code.google.com/p/benchrun/
Fibonacci numbers test benchmark
"""
from benchrun import Benchmark, clock
def fib1(n):
if n < 2:
return n
return fib1(n-1) + fib1(n-2)
def fib2(n):
if n < 2:
return n
a, b = 1, 0
for _ in range(n-1):
a, b = a+b, a
return a
class FibonacciBenchmark(Benchmark):
"""Compare time to compute the nth Fibonacci number recursively
(fib1) and iteratively (fib2)."""
# Execute for all combinations of these parameters
parameters = ['version', 'n']
version = ['fib1', 'fib2']
n = range(0, 60, 5)
# Compare timings against this parameter value
reference = ('version', 'fib1')
def run(self, n, version):
f = globals()[version]
# Don't repeat when slow
if version == 'fib1' and n > 10:
# Skip altogether
if n > 30:
return None
t1 = clock()
f(n)
t2 = clock()
return t2-t1
# Need to repeat many times to get accurate timings for small n
t1 = clock()
# pylint: disable=multiple-statements
f(n); f(n); f(n); f(n); f(n); f(n); f(n)
f(n); f(n); f(n); f(n); f(n); f(n); f(n)
t2 = clock()
return (t2 - t1) / 14
if __name__ == '__main__':
FibonacciBenchmark().print_result()