Commit 5a11685
authored
⚡️ Speed up function
Here's an optimized version of your program.
**Optimization notes:**
- The `for i in range(number * 100): k += i` loop can be replaced using the arithmetic series sum formula for integers: sum = n*(n-1)//2 (from 0 to n-1), which is *much* faster.
- `j = sum(range(number))` is similarly just `number*(number-1)//2`.
- `" ".join(str(i) for i in range(number))` can be sped up with a map object instead of a generator expression.
- The `number = number if number < 1000 else 1000` line is already optimal for a single expression.
Here is the optimized version, with all comments preserved as per your instructions.
**Function signature and all outputs remain unchanged.**
**Key speedups:**
- `k` and `j` computation now take constant time.
- `join` is as fast as possible without using C extensions.
Let me know if further improvements are required or if the join result needs to be in a different format for very large `number`!funcA by 3,983%1 parent 535a9b1 commit 5a11685
File tree
1 file changed
+13
-10
lines changed- code_to_optimize/code_directories/simple_tracer_e2e
1 file changed
+13
-10
lines changedLines changed: 13 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 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
11 | 10 | | |
12 | | - | |
13 | | - | |
| 11 | + | |
| 12 | + | |
14 | 13 | | |
15 | 14 | | |
16 | 15 | | |
| |||
21 | 20 | | |
22 | 21 | | |
23 | 22 | | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
50 | | - | |
| 51 | + | |
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| 56 | + | |
55 | 57 | | |
56 | 58 | | |
57 | 59 | | |
| |||
60 | 62 | | |
61 | 63 | | |
62 | 64 | | |
| 65 | + | |
63 | 66 | | |
64 | 67 | | |
65 | 68 | | |
0 commit comments