Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does creating separate functions instead of one big one slow processing time?

I'm working in the Google App Engine environment and programming in Python. I am creating a function that essentially generates a random number/letter string and then stores to the memcache.

def generate_random_string():
# return a random 6-digit long string

def check_and_store_to_memcache():
    randomstring = generate_random_string()
    #check against memcache
    #if ok, then store key value with another value
    #if not ok, run generate_random_string() again and check again.

Does creating two functions instead of just one big one affect performance? I prefer two, as it better matches how I think, but don't mind combining them if that's "best practice".

like image 939
ehfeng Avatar asked Jul 04 '09 22:07

ehfeng


People also ask

Do functions make code run slower?

How will it affect performance? It will either make it faster, or slower, or not change it, depending on which specific language you use and what is the structure of the actual code and possibly on what version of the compiler you're using and maybe even what platform you're running on.

Do using functions make code run faster?

Functions CAN make code faster by coding logic once instead of repeating several times and thus reducing code size and resulting in better CPU cache usage. Functions CAN make code slower by copying parameters and hiding info from the optimization.

Are functions faster?

According to the documentation and this MATLAB answer, functions are generally faster than scripts.

Does calling a function takes time Python?

The short version is that it takes about 150ns to call a function in Python (on my laptop). This doesn't sound like a lot, but it means that you can make at most 6.7 million calls per second, two to three orders of magnitude slower than your processor's clock speed.


1 Answers

In almost all cases, "inlining" functions to increase speed is like getting a hair cut to lose weight.

like image 83
Shawn J. Goff Avatar answered Sep 30 '22 13:09

Shawn J. Goff