Does it create separate functions instead of one big slow processing time?

I work in the Google App Engine and Python programming environments. I am creating a function that essentially generates a random string of number / letter and then saves it in 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 it create two functions instead of one big performance? I prefer two, as it is better suited, as I think, but do not mind combining them if this is “best practice”.

+7
performance function python google-app-engine
source share
4 answers

Focus on being able to read and easily understand your code.

Once you do this, if you have a performance problem, look at what might cause it.

Most languages, including python, tend to have fairly low overhead for making method calls. Inclusion of this code in one function will not (dramatically) change performance indicators - I would suggest that random number generation is likely to be the main part of the time without two functions.

Speaking of this, the splitting functions have a (very, very insignificant) effect on performance. However, I would think of it this way: you may need 80 mph on the highway to 79.99 mph (which you will never notice). Important things to consider are avoiding stoplights and traffic jams, as they will make you stop altogether ...

+28
source share

In almost all cases, the functions of "inlay" to increase the speed resemble a haircut for weight loss.

+14
source share
Reed is right. For the change you are considering, the cost of calling a function is a small number of cycles, and you will need to do this 10 ^ 8 or so once per second before you notice.

However, I would caution that often people go to the other extreme, and then, as if calling functions was expensive. I saw this in redesigned systems where there were many levels of abstraction.

What happens is some kind of human psychology that says that if something is easy to name, then it is fast. This leads to writing more function calls than is strictly necessary, and when this occurs at several levels of abstraction, the loss can be exponential.

Following Reed’s driving example, calling a function can be a workaround, and if there are workarounds in the workaround, and if they also have workarounds, then a huge amount of time will be spent in the near future, for no obvious reason, because each calling a function looks innocent.

+4
source share

Like others, I would not worry about this in this particular scenario. Very little overhead associated with function calls will be pale compared to what is done inside each function. And until these functions are called in quick succession, it probably doesn't matter much anyway.

This is a good question. In some cases, it is better not to break the code into several functions. For example, when working with intensified mathematical problems with nested loops, it is best to make as few function calls as possible in the inner loop. This is because simple mathematical operations alone are very cheap, and next to them, utility functions can cause a noticeable decrease in performance.

A few years ago, I discovered that the hypot (hypotenuse) function in the math library that I used in the VC ++ application was very slow. It seemed funny to me because it is such a simple set of functionality - return sqrt (a * a + b * b) - how complicated is it? So I wrote my own and was able to improve 16X performance. Then I added the keyword "inline" to the function and made it 3 times faster than this (about 50X faster at this point). Then I took the code from the function and put it in my loop and saw another slight increase in performance. So ... yes, these are the types of scenarios where you can see the difference.

+2
source share

All Articles