Program performance depends basically on two major areas one is memory and another is time. Programmer should aware of writing memory constrain and time constrain programs. Most of the optimization done at compilation level itself and some of the done at running time. Here we discuss on very small areas that really affecting our code performance.
Here i am sharing some of the techniques for beginners, which i learnt on my coding experience and reference taken from other sources. This optimizations techniques
Memory Optimization
Time optimization
Here i am sharing some of the techniques for beginners, which i learnt on my coding experience and reference taken from other sources. This optimizations techniques
Memory Optimization
- Memory allocation to your variables is a tactic. If memory has to be given to our array or string variables we follow static and dynamic memory allocation techniques.
- Static memory allocation has many issues , static memory allocate at compile time so we can not control the memory wastage if we were not used this memory efficiently. On the other hand life time of static memory depends on the program and function scope based on the memory allocation area. so before giving static memory to variables you should must have exact calculation of your requirements.
- Once memory has allocated statically it will occupy stack and other storage area till life time of program. This kind of memory occupancy will lead program in to huge memory wastage it can not provide memory reusability.
- Whereas Dynamic allocation allows program to allocate memory at run time and it can be freed after using it. Continually allocation of memory and not freeing them lead to code into memory overflow condition. so it is programmer should free dynamic pointers intelligently.
- Dynamic memory allocation where provides exact size of memory on the other hand it make cause memory leak type of serious issues.
- If it is memory allocation for structures or array of structures we should must go for dynamic memory allocation.
- If application sending any packets to network through sockets. they use one buffer to encode the packets into single body. Most of the programmer uses static memory buffers, if they use dynamic also they don't free after sending this on network, that should be freed.
- Be careful on choosing of data types for variables. i.e if variable range is between 0-255 then for this int32 or int64 data type is not required.
- Arrange variables in a certain orders like int, pointer, floating points and so forth. Because some of the compilers provide further level of optimization, it can improve code performance.
- Auto variables are stored in stack, if they are many then programmer has to move some of the auto variables in to different memory areas. by placing them in to global or using register storage class or if possible you can move them into heap.
- As much as try to avoid declaring any variable inside the loop. if it is not essential.
Code timing performance is totally depends on how the code is written
- Loop, nested loop they are major factors which causes delay in execution.
- Loop performance depends on the several parameters. Number of iteration, try to avoid infinite loops. Restrict the number of iteration, put exact condition for iteration.
- put constant calculation out side the loop.
- use bit-wise operator for certain type of arithmetic operation. ex- divided by two, multiplication of 2.
- Use look-up tables for trigonometric and logarithmic operations.
- try to avoid direct function calling within a loop use call-back functions instead.
- Make the loop blocking type. blocking loops improve the CPU performance.
- Always place data reading at the top of the loop and data writing at the end of the loop.
- Use inline functions that reduces function calling over head but increases size of code.
- use for, while and do while based on the compiler. because compiler decides the efficiency of loops.
- if else can be replaced by switch() if four or more than four items are there, then programmer can go for switch().
source: web and own experience
For time optimization u mention that , try to avoid direct function calling within a loop use call-back functions instead? Can u explain how direct function take more time cycle compare to call back function....?
ReplyDeleteActually calling a direct function always requires separate stack memory entry for each calling. so calling a function again-n-again will required more stack entry obviously. Whereas function pointer calls same function indirectly and as a result there is no stack entry for indirect calling of a function i.e. callback function. This technique basically for stack optimization to improve your code performance not only in timing wise but also makes memory efficient.
ReplyDelete