在线编译器 Python

def compiler_stress_test(n=500): import math def fibonacci(x): if x <= 1: return x return fibonacci(x - 1) + fibonacci(x - 2) def make_multiplier(x): def multiply(y): return x * y return multiply def numbers(): for i in range(n): yield i * i @lambda f: f def calculate(): squares = [x for x in numbers() if x % 2 == 0] multiplier = make_multiplier(3) total = sum( multiplier(x) for x in squares ) try: result = math.sqrt(total) except Exception as e: return str(e) return { "fibonacci": fibonacci(20), "sum": total, "sqrt": result, "count": len(squares), "checksum": sum(squares) % 1000003 } return calculate() print(compiler_stress_test())
This code defines a function that builds several small helper functions and then runs a computation combining them. It generates squares of numbers, filters even ones, multiplies them by 3, sums them, takes a square root, and also computes a Fibonacci number. The result is returned as a dictionary.

- Look at the decorator `@lambda f: f` above `calculate`. A decorator replaces the function with whatever the lambda returns. Here the lambda returns `f` unchanged, so it works, but think about what would happen if the lambda returned something else, and whether this is the intended use of a decorator.
- Check the `fibonacci` function: it is recursive and called with `fibonacci(20)`. Consider how many calls this generates and whether that could be slow or hit recursion limits, especially inside a "stress test".