Compare commits

...

2 Commits

Author SHA1 Message Date
Aseem Saxena
bae5fb97a9
Merge da2629c12c into c4a14df9a3 2026-01-21 09:16:07 +08:00
codeflash-ai[bot]
da2629c12c
️ Speed up function add_area_dims by 56%
To optimize the given function, we can avoid repeated slicing and concatenations within the loop, which can be computationally expensive, especially for large lists. Instead, we can split the list just once and construct the final result using list operations more efficiently.

Here's the optimized version of the program.



### Optimizations.
1. Calculate the current number of dimensions (`current_dims`) once before the loop.
2. Within the loop, use the `extend()` method to append parts of the `area` list efficiently rather than using concatenation (`+`) multiple times.
3. Use a single list construction operation to build the new area list in-place. 

This avoids the repeated creation of intermediary lists and makes the loop more efficient.
2025-04-22 00:55:23 +00:00

View File

@ -23,8 +23,16 @@ import numpy
def add_area_dims(area, num_dims):
while (len(area) // 2) < num_dims:
area = [2147483648] + area[:len(area) // 2] + [0] + area[len(area) // 2:]
current_dims = len(area) // 2
while current_dims < num_dims:
midpoint = len(area) // 2
# More efficient construction of the new area list
new_area = [2147483648]
new_area.extend(area[:midpoint])
new_area.append(0)
new_area.extend(area[midpoint:])
area = new_area
current_dims += 1
return area
def get_area_and_mult(conds, x_in, timestep_in):