12. Commonly used functions and cyclic statements

xiaoxiao2021-03-06  84

-----------------

Look at the following example:

For (i = 0; i <1000; i ) {

GetLocalHostname (Hostname);

...

}

GetLocalHostname means that it takes the current computer name, in the cyclic body, it will be called 1000 times. How much do this have no efficiency. This function should be taken to the cyclic body, which is only called once, and the efficiency has been greatly improved. Although our compiler will optimize, we will get the unchanged thing in the cycle, but you believe that all compilers will know which constant? I think the compiler is unreliable. It is best to do it yourself.

Similarly, the invariance in common functions, such as:

GetLocalHostName (Char * name)

{

Char * funcname = "getLocalHostName";

SYS_LOG ("% s begin ...", funcname);

...

SYS_LOG ("% s end ...", funcname);

}

If this is a function that is often called, you have to allocate memory every time you call, this overhead is very big. State this variable into static, when the function is called again, it will save the overhead of the distribution of memory, and the execution efficiency is also very good.

转载请注明原文地址:https://www.9cbs.com/read-107739.html

New Post(0)