YugJ commited on
Commit
32ecc14
·
verified ·
1 Parent(s): 531ad36

Upload examples.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. examples.py +151 -0
examples.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Benchmark programs offered as one-click presets.
2
+
3
+ Each is chosen to be a fair test of translation rather than a trick: pure
4
+ computation, deterministic output, no I/O or third-party imports, and a runtime
5
+ long enough in Python that the speedup is unambiguous.
6
+
7
+ The output is printed so the two languages can be compared exactly - a
8
+ translation that is fast but wrong is a failed translation.
9
+ """
10
+
11
+ MAX_SUBARRAY = '''# Brute-force maximum subarray sum over a seeded pseudo-random sequence.
12
+ # O(n^2) by design - the interesting question is whether the model preserves
13
+ # the algorithm or silently upgrades it.
14
+
15
+ def lcg(seed, a=1664525, c=1013904223, m=2**32):
16
+ value = seed
17
+ while True:
18
+ value = (a * value + c) % m
19
+ yield value
20
+
21
+ def max_subarray_sum(n, seed, min_val, max_val):
22
+ lcg_gen = lcg(seed)
23
+ random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]
24
+ max_sum = float('-inf')
25
+ for i in range(n):
26
+ current_sum = 0
27
+ for j in range(i, n):
28
+ current_sum += random_numbers[j]
29
+ if current_sum > max_sum:
30
+ max_sum = current_sum
31
+ return max_sum
32
+
33
+ def total_max_subarray_sum(n, initial_seed, min_val, max_val):
34
+ total_sum = 0
35
+ lcg_gen = lcg(initial_seed)
36
+ for _ in range(20):
37
+ seed = next(lcg_gen)
38
+ total_sum += max_subarray_sum(n, seed, min_val, max_val)
39
+ return total_sum
40
+
41
+ n = 10000
42
+ initial_seed = 42
43
+ min_val = -10
44
+ max_val = 10
45
+
46
+ import time
47
+ start_time = time.time()
48
+ result = total_max_subarray_sum(n, initial_seed, min_val, max_val)
49
+ end_time = time.time()
50
+
51
+ print("Total Maximum Subarray Sum (20 runs):", result)
52
+ print("Execution Time: {:.6f} seconds".format(end_time - start_time))
53
+ '''
54
+
55
+ PI_LEIBNIZ = '''# Tight floating-point loop. Almost pure interpreter overhead in Python,
56
+ # so it shows the raw cost of dynamic dispatch per iteration.
57
+
58
+ import time
59
+
60
+ def calculate(iterations, param1, param2):
61
+ result = 1.0
62
+ for i in range(1, iterations + 1):
63
+ j = i * param1 - param2
64
+ result -= (1 / j)
65
+ j = i * param1 + param2
66
+ result += (1 / j)
67
+ return result
68
+
69
+ start_time = time.time()
70
+ result = calculate(100_000_000, 4, 1) * 4
71
+ end_time = time.time()
72
+
73
+ print(f"Result: {result:.12f}")
74
+ print(f"Execution Time: {end_time - start_time:.6f} seconds")
75
+ '''
76
+
77
+ PRIME_SIEVE = '''# Sieve of Eratosthenes plus a digit-sum reduction.
78
+ # Memory-bound rather than compute-bound, so it stresses a different axis
79
+ # than the other two.
80
+
81
+ import time
82
+
83
+ def sieve(limit):
84
+ flags = [True] * (limit + 1)
85
+ flags[0] = flags[1] = False
86
+ p = 2
87
+ while p * p <= limit:
88
+ if flags[p]:
89
+ for multiple in range(p * p, limit + 1, p):
90
+ flags[multiple] = False
91
+ p += 1
92
+ return flags
93
+
94
+ def digit_sum(n):
95
+ total = 0
96
+ while n:
97
+ total += n % 10
98
+ n //= 10
99
+ return total
100
+
101
+ start_time = time.time()
102
+ limit = 5_000_000
103
+ flags = sieve(limit)
104
+ count = 0
105
+ checksum = 0
106
+ for n in range(limit + 1):
107
+ if flags[n]:
108
+ count += 1
109
+ checksum += digit_sum(n)
110
+ end_time = time.time()
111
+
112
+ print(f"Primes below {limit}: {count}")
113
+ print(f"Digit-sum checksum: {checksum}")
114
+ print(f"Execution Time: {end_time - start_time:.6f} seconds")
115
+ '''
116
+
117
+ COLLATZ = '''# Longest Collatz chain below a bound. Unpredictable branching, which
118
+ # defeats naive vectorisation and rewards a good compiler.
119
+
120
+ import time
121
+
122
+ def chain_length(n):
123
+ steps = 0
124
+ while n != 1:
125
+ n = n // 2 if n % 2 == 0 else 3 * n + 1
126
+ steps += 1
127
+ return steps
128
+
129
+ start_time = time.time()
130
+ limit = 1_000_000
131
+ best_n = 0
132
+ best_len = 0
133
+ for n in range(1, limit):
134
+ length = chain_length(n)
135
+ if length > best_len:
136
+ best_len = length
137
+ best_n = n
138
+ end_time = time.time()
139
+
140
+ print(f"Longest Collatz chain below {limit}: n={best_n} with {best_len} steps")
141
+ print(f"Execution Time: {end_time - start_time:.6f} seconds")
142
+ '''
143
+
144
+ EXAMPLES: dict[str, str] = {
145
+ "Maximum subarray sum (O(n^2))": MAX_SUBARRAY,
146
+ "Pi via Leibniz series (100M iterations)": PI_LEIBNIZ,
147
+ "Prime sieve + digit sums (5M)": PRIME_SIEVE,
148
+ "Longest Collatz chain (1M)": COLLATZ,
149
+ }
150
+
151
+ DEFAULT_EXAMPLE = "Maximum subarray sum (O(n^2))"