“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
- Code level optimization
such as
1. Optimize function calls
- use fast call which doesn’t use the stack but uses some processor registers. this is limited only when the parameters are not greater than 64 bit(i think)
2. Optimize repeated calls
Use inline functions and macros instead of calling functions for small procedure and abuse the stack
3. Allocate memory all at once
instead of re-allocating memory each time you add an element
4. use objects instead of heap pointers as possible as you can
5. be wise in using loops
for example
for (i = 10; i >=0; i–)
is faster than
for(i = 0; i
It is also important to know what are the goals of optimization - smaller code, faster execution, less memory consumption? So there might be opposite views…
Use pass by reference whereever possible.
Indexing, hashing for faster access.
Use arrays than linked list (if possible)
Reference is better than pointers
Restrict use of static variables(memory optimization)
1. Find the bottleneck. It’s more effective to optimize the bottleneck than any other part of the program. Also, you often get a larger improvement for your effort if you focus on inner loops, i.e. parts of the code that are executed frequently. Running a profiling tool helps a lot here.
2. Remove unecessary steps.
a. Cache results of an expensive process if you are going to use them again rather than calling the same function to recreate the same results.
b. Believe it or not, we sometimes write code that (hidden within the details) produces intermediate results that we never actually make use of. Find those places in your code and get rid of them.
c. Reduce, reuse, recycle. You can actually gain performance in some cases if you don’t create a new object every time you need a new object. You can simply construct one object, but reassign the members of that object more efficiently.
d. If your bottleneck involves copying large objects, consider a shallow copy instead of a deep copy.
3. Replace a slow module with a faster one that does the same function. This is an option when the bottleneck is inside a class or function that you cannot change and there are faster alternatives that do the same thing. This includes the following:
a. using a faster programming language,
b. using a different library container class with better performance properties (e.g. an array has O(1) lookup time, while a linked list might have O(n) lookup time, so if your bottleneck involves looking up something in a sequential container, then using an array would be a lot faster, or a hash table as was mentioned).
c. pass pointers or references around rather than whole objects.
4. Redesign the algorithm to be more efficient. Sometimes you have to go back to the drawing board and attack the problem in a completely different way.
there are machine dependent as well as machine independent code optimization techniques.
1. machine dependent techniques:
local optimization where the use of registers is done efiiceiently.
Eg:
for stmt like sum= value*60
R1=60;
R2= value*R1;
sum=R2;
can be written as R1= value*60;
sum=R1,’ so only 1 register is used.
2. machine independent techniques are:
1. Loop optimization
here move those stmts which are not required in the loop outside the loop
2. Loop Unrolling
here reduce the no of tests in a loop
3.loop jamming
here 2 bodies of loops are merged into 1 if they have common no of iterations
eg: for(i=1,I
Use a profiler and be specific with the Profiling library you plan to use. Ie: if the position is for Java name a comon profiler to find the bottlenecks.
After cleaning the code also mention you plan to ensure a case-testing framework (ie: JUnit) so that it is obvious you are also thinking on the preventative side.
the easiest and most common optimization technique used is compiler optimization, which is done just by setting a flag, and it takes care of most of the optimization discussed by all the people on this question.
bar code reader
- Code level optimization
such as
1. Optimize function calls
- use fast call which doesn’t use the stack but uses some processor registers. this is limited only when the parameters are not greater than 64 bit(i think)
2. Optimize repeated calls
Use inline functions and macros instead of calling functions for small procedure and abuse the stack
3. Allocate memory all at once
instead of re-allocating memory each time you add an element
4. use objects instead of heap pointers as possible as you can
5. be wise in using loops
for example
for (i = 10; i >=0; i–)
is faster than
for(i = 0; i
It is also important to know what are the goals of optimization - smaller code, faster execution, less memory consumption? So there might be opposite views…
Use pass by reference whereever possible.
Indexing, hashing for faster access.
Use arrays than linked list (if possible)
Reference is better than pointers
Restrict use of static variables(memory optimization)
Assuming we are optimizing the run-time:
1. Find the bottleneck. It’s more effective to optimize the bottleneck than any other part of the program. Also, you often get a larger improvement for your effort if you focus on inner loops, i.e. parts of the code that are executed frequently. Running a profiling tool helps a lot here.
2. Remove unecessary steps.
a. Cache results of an expensive process if you are going to use them again rather than calling the same function to recreate the same results.
b. Believe it or not, we sometimes write code that (hidden within the details) produces intermediate results that we never actually make use of. Find those places in your code and get rid of them.
c. Reduce, reuse, recycle.
You can actually gain performance in some cases if you don’t create a new object every time you need a new object. You can simply construct one object, but reassign the members of that object more efficiently.
d. If your bottleneck involves copying large objects, consider a shallow copy instead of a deep copy.
3. Replace a slow module with a faster one that does the same function. This is an option when the bottleneck is inside a class or function that you cannot change and there are faster alternatives that do the same thing. This includes the following:
a. using a faster programming language,
b. using a different library container class with better performance properties (e.g. an array has O(1) lookup time, while a linked list might have O(n) lookup time, so if your bottleneck involves looking up something in a sequential container, then using an array would be a lot faster, or a hash table as was mentioned).
c. pass pointers or references around rather than whole objects.
4. Redesign the algorithm to be more efficient. Sometimes you have to go back to the drawing board and attack the problem in a completely different way.
there are machine dependent as well as machine independent code optimization techniques.
1. machine dependent techniques:
local optimization where the use of registers is done efiiceiently.
Eg:
for stmt like sum= value*60
R1=60;
R2= value*R1;
sum=R2;
can be written as R1= value*60;
sum=R1,’ so only 1 register is used.
2. machine independent techniques are:
1. Loop optimization
here move those stmts which are not required in the loop outside the loop
2. Loop Unrolling
here reduce the no of tests in a loop
3.loop jamming
here 2 bodies of loops are merged into 1 if they have common no of iterations
eg: for(i=1,I
Use a profiler and be specific with the Profiling library you plan to use. Ie: if the position is for Java name a comon profiler to find the bottlenecks.
After cleaning the code also mention you plan to ensure a case-testing framework (ie: JUnit) so that it is obvious you are also thinking on the preventative side.
the easiest and most common optimization technique used is compiler optimization, which is done just by setting a flag, and it takes care of most of the optimization discussed by all the people on this question.
Leave an Answer/Comment