From the course: Faster Python Code

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Allocate

Allocate - Python Tutorial

From the course: Faster Python Code

Start my 1-month free trial

Allocate

- [Narrator] Let's say we have a pool of workers and we'd like to know how many tasks each worker has done. We can start with a list in the length of the workers, filled with zeros, and have each worker increment their value in their index. This allocation can happen many time if you frequently allocate the pool of workers. Let's write a function to create this list. The code is in alloc.py. So in line four, we define allocz which creates a list filled with zero for a given size. And let's time it, ipython and then we run alloc.py. And let's time it of 1000. How can we do better? In Python, if you multiply a list with a number, we get n copies of this list. For example, if I take the list of one, two, and three and I multiply it by three, I'll get this list three times. Let's use this in our code. In a new code, I wrote a function called allocz fixed, which returns a list of one element with zero times the size. Don't…

Contents