performance

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

How is that pandas is faster than pure C in the groupby operation?

How is that pandas is faster than pure C in the groupby operation? Question: I have an nparray of x,y pairs with shape (n,2), and knowing for certain that for each x there are multiple values of y , I wanted to calculate the average value of y for each unique x. It occurred to …

Total answers: 1

I need help to speed up of a Python for-loop with huge amount of calculations

I need help to speed up of a Python for-loop with huge amount of calculations Question: I am working on a pice of Python software that requires to run a huge amount of calculations. I am talking about up to hundred of millions of calculations or more (n in below code can be 100000 or …

Total answers: 3

Dataframe of start and end dates into sum of days in an array of periods

Dataframe of start and end dates into sum of days in an array of periods Question: I have a pandas data frame of start and end dates for contracts. I want to work out the number of in force contract days for all periods (e.g. months) covered by the contracts. Example input: start_date end_date 0 …

Total answers: 1