From the course: Faster Python Code

Unlock the full course today

Join today to access over 23,000 courses taught by industry experts.

Remove function calls

Remove function calls - Python Tutorial

From the course: Faster Python Code

Remove function calls

- [Instructor] Functions are a great way to organize your code. You get modularity, re-usability, readability, and more. You might have seen this acronym, T-A-N-S-T-A-A-F-L. It means there ain't no such thing as a free lunch. There is a cost to functions, and it's the overhead of calling them. How much time are we talking about? Let's see. So ipython, we'll define an empty function that does nothing, and we'll use timeit to see how much time it takes to call it. So 66.3 nanoseconds, which is pretty fast, but still considered a lot for a function call. Here's an example. We'd like to fix a list of numbers and make the even numbers absolute. So we define a function for abs_even in line four, and then in line 17 we just do a list comprehension to create this number. And in line 22 I'm creating such a list of numbers so we can time it. Let's benchmark. So run fncall.py, and then we did timeit on fix_nums of…

Contents