Commit 9600fb5
authored
⚡️ Speed up function
To optimize the given Python program, the following modifications are applied.
1. **Avoid Repeated Subprocess Calls**.
The program repeatedly calls the `get_log` to get logs, which is not efficient. Instead, we can avoid multiple calls by reusing the results.
2. **Combine try-except with check_call**.
For adding the "upstream" remote, combine the exception handling with conditional logic to reduce redundancy.
The refactored version of the program is presented below.
### Explanation of Changes.
1. **Avoid Redundant Computations**: The `ref_args` and `n_args` lists are computed in a more compact and direct way.
2. **Use `days` Argument for Timedelta**: Instead of `365 * 2`, I used the `days` argument for better readability and replaced `datetime.timedelta(365 * 2)` with `datetime.timedelta(days=365 * 2)`.
3. **Reuse Result of `get_log`**: The result of `get_log` for `%H` and `%cI` is computed once and reused where needed, eliminating redundant computation.
4. **Combine `try-except` and Check Conditions**: Combined the handling of adding the “upstream” remote in a single block, only catching exceptions when necessary.
These modifications result in a code that runs more efficiently and is easier to read and maintain.get_git_merge_base by 6%1 parent 229d461 commit 9600fb5
1 file changed
Lines changed: 11 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
41 | 36 | | |
42 | 37 | | |
43 | 38 | | |
| |||
64 | 59 | | |
65 | 60 | | |
66 | 61 | | |
67 | | - | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
68 | 65 | | |
69 | 66 | | |
70 | 67 | | |
| |||
79 | 76 | | |
80 | 77 | | |
81 | 78 | | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
| 79 | + | |
86 | 80 | | |
87 | 81 | | |
88 | 82 | | |
| |||
96 | 90 | | |
97 | 91 | | |
98 | 92 | | |
| 93 | + | |
99 | 94 | | |
100 | 95 | | |
101 | 96 | | |
| |||
107 | 102 | | |
108 | 103 | | |
109 | 104 | | |
| 105 | + | |
110 | 106 | | |
111 | | - | |
112 | | - | |
| 107 | + | |
| 108 | + | |
113 | 109 | | |
114 | 110 | | |
115 | 111 | | |
| |||
0 commit comments