performance

Given two numpy arrays, how to split one into an array of lists based on the second

Given two numpy arrays, how to split one into an array of lists based on the second Question: I have two numpy arrays: one containing arbitrary values, and one containing integers larger than 1. The sum of the integers is equal to the length of the first array. Sample: values = np.array(["a", "b", "c", "d", …

Total answers: 1

How can I make this Indexing algorithm more efficient?

How can I make this Indexing algorithm more efficient? Question: I’ve got a Dataframe (deriving from a csv file with various columns) with 172033 rows. I’ve created a custom indexing function that blocks pairs of records that haven’t got similar ‘name’ attributes. The problem resides in the efficiency of the algorithm. Just to get to …

Total answers: 4

Why is b.pop(0) over 200 times slower than del b[0] for bytearray?

Why is b.pop(0) over 200 times slower than del b[0] for bytearray? Question: Letting them compete three times (a million pops/dels each time): from timeit import timeit for _ in range(3): t1 = timeit(‘b.pop(0)’, ‘b = bytearray(1000000)’) t2 = timeit(‘del b[0]’, ‘b = bytearray(1000000)’) print(t1 / t2) Time ratios (Try it online!): 274.6037053753368 219.38099365582403 252.08691226683823 …

Total answers: 3

Is using else faster than returning value right away?

Is using else faster than returning value right away? Question: Which of the following is faster? 1. def is_even(num: int): if num%2==0: return True else: return False def is_even(num: int): if num%2==0: return True return False I know you can technically do this: def is_even(num: int): return n%2==0 But for the sake of the question, …

Total answers: 1

How can i make this python code quicklier?

How can i make this python code quicklier? Question: How can i make this code more quicklier? def add_may_go(x,y): counter = 0 for i in range(-2,3): cur_y = y + i if cur_y < 0 or cur_y >= board_order: continue for j in range(-2,3): cur_x = x+j if (i == 0 and j == 0) …

Total answers: 3

I don't understand why is this for loop so fast?

I don't understand why is this for loop so fast? Question: Today I was solving Project Euler’s problem #43 Problem and I ran into a somewhat interesting problem. I don’t understand why is my code so fast? from itertools import permutations def main(): numbers1_9 = [0,1,2,3,4,5,6,7,8,9] list_of_all_permutations = list(permutations(numbers1_9, 10)) length_of_my_list = len(list_of_all_permutations) number_of_times_it_ran=0 result …

Total answers: 1

Multithreaded python program performance loss after laptop upgrade

Multithreaded python program performance loss after laptop upgrade Question: I have been having this weird issue where my new laptop handles an albeit computationally intensive program I wrote worse than my last one. The code is a python program I wrote that measures errors in a numerical model for a large set of data. It …

Total answers: 2

Same python commands take different time if called in different ways

Same python commands take different time if called in different ways Question: I have some data (bytes) represending a png image that come over a pyzmq socket from an executable running in the background. I transform these to a numpy array using the following command decoded = np.asarray(im.open(io.BytesIO(data))) When I do this from the command …

Total answers: 1

Simulation of Markov chain slower than in Matlab

Simulation of Markov chain slower than in Matlab Question: I run the same test code in Python+Numpy and in Matlab and see that the Matlab code is faster by an order of magnitude. I want to know what is the bottleneck of the Python code and how to speed it up. I run the following …

Total answers: 1