Spaces:
Running on Zero
Running on Zero
| """Benchmark programs offered as one-click presets. | |
| Each is chosen to be a fair test of translation rather than a trick: pure | |
| computation, deterministic output, no I/O or third-party imports, and a runtime | |
| long enough in Python that the speedup is unambiguous. | |
| The output is printed so the two languages can be compared exactly - a | |
| translation that is fast but wrong is a failed translation. | |
| """ | |
| MAX_SUBARRAY = '''# Brute-force maximum subarray sum over a seeded pseudo-random sequence. | |
| # O(n^2) by design - the interesting question is whether the model preserves | |
| # the algorithm or silently upgrades it. | |
| def lcg(seed, a=1664525, c=1013904223, m=2**32): | |
| value = seed | |
| while True: | |
| value = (a * value + c) % m | |
| yield value | |
| def max_subarray_sum(n, seed, min_val, max_val): | |
| lcg_gen = lcg(seed) | |
| random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)] | |
| max_sum = float('-inf') | |
| for i in range(n): | |
| current_sum = 0 | |
| for j in range(i, n): | |
| current_sum += random_numbers[j] | |
| if current_sum > max_sum: | |
| max_sum = current_sum | |
| return max_sum | |
| def total_max_subarray_sum(n, initial_seed, min_val, max_val): | |
| total_sum = 0 | |
| lcg_gen = lcg(initial_seed) | |
| for _ in range(20): | |
| seed = next(lcg_gen) | |
| total_sum += max_subarray_sum(n, seed, min_val, max_val) | |
| return total_sum | |
| n = 10000 | |
| initial_seed = 42 | |
| min_val = -10 | |
| max_val = 10 | |
| import time | |
| start_time = time.time() | |
| result = total_max_subarray_sum(n, initial_seed, min_val, max_val) | |
| end_time = time.time() | |
| print("Total Maximum Subarray Sum (20 runs):", result) | |
| print("Execution Time: {:.6f} seconds".format(end_time - start_time)) | |
| ''' | |
| PI_LEIBNIZ = '''# Tight floating-point loop. Almost pure interpreter overhead in Python, | |
| # so it shows the raw cost of dynamic dispatch per iteration. | |
| import time | |
| def calculate(iterations, param1, param2): | |
| result = 1.0 | |
| for i in range(1, iterations + 1): | |
| j = i * param1 - param2 | |
| result -= (1 / j) | |
| j = i * param1 + param2 | |
| result += (1 / j) | |
| return result | |
| start_time = time.time() | |
| result = calculate(100_000_000, 4, 1) * 4 | |
| end_time = time.time() | |
| print(f"Result: {result:.12f}") | |
| print(f"Execution Time: {end_time - start_time:.6f} seconds") | |
| ''' | |
| PRIME_SIEVE = '''# Sieve of Eratosthenes plus a digit-sum reduction. | |
| # Memory-bound rather than compute-bound, so it stresses a different axis | |
| # than the other two. | |
| import time | |
| def sieve(limit): | |
| flags = [True] * (limit + 1) | |
| flags[0] = flags[1] = False | |
| p = 2 | |
| while p * p <= limit: | |
| if flags[p]: | |
| for multiple in range(p * p, limit + 1, p): | |
| flags[multiple] = False | |
| p += 1 | |
| return flags | |
| def digit_sum(n): | |
| total = 0 | |
| while n: | |
| total += n % 10 | |
| n //= 10 | |
| return total | |
| start_time = time.time() | |
| limit = 5_000_000 | |
| flags = sieve(limit) | |
| count = 0 | |
| checksum = 0 | |
| for n in range(limit + 1): | |
| if flags[n]: | |
| count += 1 | |
| checksum += digit_sum(n) | |
| end_time = time.time() | |
| print(f"Primes below {limit}: {count}") | |
| print(f"Digit-sum checksum: {checksum}") | |
| print(f"Execution Time: {end_time - start_time:.6f} seconds") | |
| ''' | |
| COLLATZ = '''# Longest Collatz chain below a bound. Unpredictable branching, which | |
| # defeats naive vectorisation and rewards a good compiler. | |
| import time | |
| def chain_length(n): | |
| steps = 0 | |
| while n != 1: | |
| n = n // 2 if n % 2 == 0 else 3 * n + 1 | |
| steps += 1 | |
| return steps | |
| start_time = time.time() | |
| limit = 1_000_000 | |
| best_n = 0 | |
| best_len = 0 | |
| for n in range(1, limit): | |
| length = chain_length(n) | |
| if length > best_len: | |
| best_len = length | |
| best_n = n | |
| end_time = time.time() | |
| print(f"Longest Collatz chain below {limit}: n={best_n} with {best_len} steps") | |
| print(f"Execution Time: {end_time - start_time:.6f} seconds") | |
| ''' | |
| EXAMPLES: dict[str, str] = { | |
| "Maximum subarray sum (O(n^2))": MAX_SUBARRAY, | |
| "Pi via Leibniz series (100M iterations)": PI_LEIBNIZ, | |
| "Prime sieve + digit sums (5M)": PRIME_SIEVE, | |
| "Longest Collatz chain (1M)": COLLATZ, | |
| } | |
| DEFAULT_EXAMPLE = "Maximum subarray sum (O(n^2))" | |