Created
May 29, 2023 18:33
-
-
Save isaacharrisholt/1c2f2760a2d869f3ef86ca5b977718c5 to your computer and use it in GitHub Desktop.
Fibonacci - Python implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from timeit import timeit | |
RUNS = 100 | |
def fibonacci(n: int) -> int: | |
if n <= 1: | |
return n | |
return fibonacci(n - 1) + fibonacci(n - 2) | |
def main(): | |
n = int(sys.argv[1]) | |
print(f"{fibonacci(n) = }") | |
python_time_per_call = timeit(lambda: fibonacci(n), number=RUNS) / RUNS | |
print(f"\nPython μs per call: {python_time_per_call * 1_000_000:.2f} μs") | |
print(f"Python ms per call: {python_time_per_call * 1_000:.2f} ms") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment