performance

Efficient way to develop a dictionary for reverse lookup?

Efficient way to develop a dictionary for reverse lookup? Question: Let’s say I have a dictionary with the following contents: old_dict = {‘a’:[0,1,2], ‘b’:[1,2,3]} and I want to obtain a new dictionary where the keys are the values in the old dictionary, and the new values are the keys from the old dictionary, i.e.: new_dict …

Total answers: 1

Problem with looping over pandas dataframe

Problem with looping over pandas dataframe Question: I have a dataframe (df) of 100 (will be more eventually) rows and 2689 columns. I have to apply a function say fn1(df,a,b) on each row. The fn1 itself has a for loop, which I cannot avoid. So, I want to speed up applying fn1 to all rows …

Total answers: 2

Is there a Python library that shows execution time per line?

Is there a Python library that shows execution time per line? Question: I have found this library called heartrate which shows how many times each line has been hit. I’m wondering if there is a similar library that can show the execution time of each line? Sometimes a single line can be a bottleneck of …

Total answers: 2

How to fix multiple if codes in python

How to fix multiple if codes in python Question: My python code is ugly. How to tune this code? find_list1 = re.findall(p1, path) find_list2 = re.findall(p2, path) find_list3 = re.findall(p3, path) if len(find_list1) > 0:find_list = find_list1 if len(find_list2) > 0:find_list = find_list2 if len(find_list3) > 0:find_list = find_list3 if len(re.findall(p1, path)) > 0:date_str1 += …

Total answers: 1

Why calling assigned lambda is faster than calling function and lambda (not assigned)?

Why calling assigned lambda is faster than calling function and lambda (not assigned)? Question: Why calling an assigned lambda in python3.11 is faster than: calling function lambda that isn’t assigned Why this happens only (maybe) on python 3.11 and not 3.7? I tried this: Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit …

Total answers: 1

Numpy array copy slower than Python list copy

Numpy array copy slower than Python list copy Question: I’ve seen several posts here about accessing individual items in numpy arrays and python lists via a for loop. My program is a little different. What I’m doing is copying a small array (or list) of about 10 elements and then using it. I’m doing this …

Total answers: 2

Why are for-loops much slower than broadcasting

Why are for-loops much slower than broadcasting Question: Comparing two chunks of code for a simple matrix operation, the one with a nested for loop is much slower. I wonder: what is the underlying reason for this? This loop tuns for 2.5 seconds: m = np.zeros((800,8000)) for i in range(0,800): for j in range(0,8000): m[i,j] …

Total answers: 1

How do I add a skip condition to a for loop without slowing it down?

How do I add a skip condition to a for loop without slowing it down? Question: I am writing a function which processes a big list of elements, and want to add the functionality that certain elements are skipped depending on an input argument. Here’s what the code looks like: def f(lst, skipsEnabled=False): for elem …

Total answers: 3