#!/usr/bin/env python3 import time def fibonacci(n): if n <= 1: return n return fibonacci(n - 1) + fibonacci(n - 2) def fibonacci_iterative(n): if n <= 1: return n a, b = 0, 1 for i in range(2, n + 1): a, b = b, a + b return b def count_primes(limit): count = 0 for n in range(2, limit + 1): is_prime = True i = 2 while i * i <= n: if n % i == 0: is_prime = False break i += 1 if is_prime: count += 1 return count def sum_loop(iterations): total = 0 for i in range(iterations): total += i return total def array_sum(size): arr = [0] * size for i in range(size): arr[i] = i total = 0 for i in range(size): total += arr[i] return total def array_reverse(size): arr = [0] * size for i in range(size): arr[i] = i for i in range(size // 2): arr[i], arr[size - 1 - i] = arr[size - 1 - i], arr[i] return arr[0] def nested_loops(n): total = 0 for i in range(n): for j in range(n): total += i + j return total def factorial(n): if n <= 1: return 1 return n * factorial(n - 1) def ackermann(m, n): if m == 0: return n + 1 if n == 0: return ackermann(m - 1, 1) return ackermann(m - 1, ackermann(m, n - 1)) def method_call_overhead(iterations): total = 0 for i in range(iterations): total += identity(i) return total def identity(x): return x def arithmetic_ops(iterations): result = 0 for i in range(1, iterations): result += i * 2 - i // 2 + i % 7 return result def conditional_branching(iterations): total = 0 for i in range(iterations): if i % 2 == 0: total += i else: total -= i return total def complex_conditions(iterations): count = 0 for i in range(iterations): if i % 3 == 0 and i % 5 == 0: count += 1 elif i % 3 == 0: count += 2 elif i % 5 == 0: count += 3 return count def matrix_multiply(size): a = [[0] * size for _ in range(size)] b = [[0] * size for _ in range(size)] c = [[0] * size for _ in range(size)] for i in range(size): for j in range(size): a[i][j] = i + j b[i][j] = i - j for i in range(size): for j in range(size): total = 0 for k in range(size): total += a[i][k] * b[k][j] c[i][j] = total return c[0][0] def bubble_sort(size): arr = [0] * size for i in range(size): arr[i] = size - i for i in range(size - 1): for j in range(size - i - 1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr[0] def main(): print("=== Python Comprehensive Benchmark Suite ===") start = time.perf_counter_ns() fib30 = fibonacci(30) end = time.perf_counter_ns() fib_time = (end - start) // 1000000 print(f"Fibonacci(30) recursive = {fib30}") print(f"Time: {fib_time} ms") start = time.perf_counter_ns() fib40_iter = fibonacci_iterative(40) end = time.perf_counter_ns() fib_iter_time = (end - start) // 1000000 print(f"Fibonacci(40) iterative = {fib40_iter}") print(f"Time: {fib_iter_time} ms") start = time.perf_counter_ns() primes = count_primes(100000) end = time.perf_counter_ns() prime_time = (end - start) // 1000000 print(f"Primes up to 100000 = {primes}") print(f"Time: {prime_time} ms") start = time.perf_counter_ns() sum_result = sum_loop(10000000) end = time.perf_counter_ns() sum_time = (end - start) // 1000000 print(f"Sum 0..10000000 = {sum_result}") print(f"Time: {sum_time} ms") start = time.perf_counter_ns() arr_sum = array_sum(1000000) end = time.perf_counter_ns() arr_sum_time = (end - start) // 1000000 print(f"Array sum (1M elements) = {arr_sum}") print(f"Time: {arr_sum_time} ms") start = time.perf_counter_ns() arr_rev = array_reverse(1000000) end = time.perf_counter_ns() arr_rev_time = (end - start) // 1000000 print(f"Array reverse (1M elements) = {arr_rev}") print(f"Time: {arr_rev_time} ms") start = time.perf_counter_ns() nested = nested_loops(1000) end = time.perf_counter_ns() nested_time = (end - start) // 1000000 print(f"Nested loops (1000x1000) = {nested}") print(f"Time: {nested_time} ms") start = time.perf_counter_ns() fact = factorial(15) end = time.perf_counter_ns() fact_time = (end - start) // 1000000 print(f"Factorial(15) recursive = {fact}") print(f"Time: {fact_time} ms") start = time.perf_counter_ns() ack = ackermann(3, 6) end = time.perf_counter_ns() ack_time = (end - start) // 1000000 print(f"Ackermann(3, 6) = {ack}") print(f"Time: {ack_time} ms") start = time.perf_counter_ns() method_call = method_call_overhead(10000000) end = time.perf_counter_ns() method_call_time = (end - start) // 1000000 print(f"Method calls (10M) = {method_call}") print(f"Time: {method_call_time} ms") start = time.perf_counter_ns() arith = arithmetic_ops(10000000) end = time.perf_counter_ns() arith_time = (end - start) // 1000000 print(f"Arithmetic ops (10M) = {arith}") print(f"Time: {arith_time} ms") start = time.perf_counter_ns() cond = conditional_branching(10000000) end = time.perf_counter_ns() cond_time = (end - start) // 1000000 print(f"Conditional branches (10M) = {cond}") print(f"Time: {cond_time} ms") start = time.perf_counter_ns() complex = complex_conditions(10000000) end = time.perf_counter_ns() complex_time = (end - start) // 1000000 print(f"Complex conditions (10M) = {complex}") print(f"Time: {complex_time} ms") start = time.perf_counter_ns() matrix = matrix_multiply(50) end = time.perf_counter_ns() matrix_time = (end - start) // 1000000 print(f"Matrix multiply (50x50) = {matrix}") print(f"Time: {matrix_time} ms") start = time.perf_counter_ns() bubble = bubble_sort(5000) end = time.perf_counter_ns() bubble_time = (end - start) // 1000000 print(f"Bubble sort (5000 elements) = {bubble}") print(f"Time: {bubble_time} ms") print("=== Benchmark Complete ===") if __name__ == "__main__": main()