Commit 89014be
authored
⚡️ Speed up function
Here is an optimized version of your `funcA` function, preserving the result and behavior, but with significantly improved runtime for all major hot spots indicated in your profile.
The slowest parts are the explicit `for` loop and `" ".join(str(i) ...)` construction. The for-loop is simply summing; we can use the arithmetic formula for summing `0..N-1`. For joining, generate all the needed numbers as strings, then join (avoiding repeated generator and multiple function calls).
**Key optimizations**.
- Used arithmetic sum for `k` instead of a for loop, reducing O(N) to O(1).
- Used the same formula for `j`.
- Used `map(str, range(number))` directly in join, which is faster than a generator with `str(i)`.
**Timing Impact**: Nearly all runtime was spent in the explicit `for` loop and string join; both are now as fast as possible in pure Python.
Return value and side effects are untouched.
All original comments were either obsolete due to the optimization or are not included as per instructions, unless affected by the code rewrite.funcA by 4,082%1 parent 7f4c01e commit 89014be
File tree
1 file changed
+16
-10
lines changed- code_to_optimize/code_directories/simple_tracer_e2e
1 file changed
+16
-10
lines changedLines changed: 16 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
| 5 | + | |
11 | 6 | | |
12 | | - | |
13 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
| |||
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| 26 | + | |
24 | 27 | | |
25 | 28 | | |
26 | 29 | | |
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
30 | 33 | | |
31 | | - | |
| 34 | + | |
32 | 35 | | |
33 | 36 | | |
34 | 37 | | |
| |||
43 | 46 | | |
44 | 47 | | |
45 | 48 | | |
| 49 | + | |
46 | 50 | | |
47 | 51 | | |
48 | 52 | | |
49 | 53 | | |
50 | | - | |
| 54 | + | |
51 | 55 | | |
52 | 56 | | |
53 | 57 | | |
54 | 58 | | |
| 59 | + | |
55 | 60 | | |
56 | 61 | | |
57 | 62 | | |
| |||
60 | 65 | | |
61 | 66 | | |
62 | 67 | | |
| 68 | + | |
63 | 69 | | |
64 | 70 | | |
65 | 71 | | |
0 commit comments