This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Code efficiancy

Hi all,
My code is based on C and I have a lot of cases where I assign a value to a variable. Is it more efficiency to just place the value or first to check if the variable already has this value?
I made two identical sample codes where in one code I had only an assignment of a value and in the other one I had verification first. I see that the CODE area in the second sample was bigger then the first one.
If CODE area is smaller, it's not mean that all code is more efficiency.
Any ideas?

Ruven

Parents
  • As I understood from yours explanations, in case where the memory is an external SRAM with few nano seconds access time it will be more efficient to just assign the value and not to check if it's already assigned to the variable.
    It is not the access time which matters, it is the difference between read time and write time. Only if the write time is greatly longer than the read time would it help to do a read first before writing. Otherwise, if the compiler doesn't throw away your code, it will be slower than if you left it alone!

    I prefer not to be dependent on compiler decisions, so in cases where I can help the compiler take decision, I do so.
    On the other hand, if you try to do things to help, without really understanding what is happening under the hood, you could do more harm than good.
    It is best to analyse any algorythms you are using for efficiency, and let the compiler optimise low level stuff. If you are really concerned about efficiency, look at the assembly code produced for the most often used routines, and if necessary, rewrite them in assembler if you can see something inefficient the compiler is doing.

Reply
  • As I understood from yours explanations, in case where the memory is an external SRAM with few nano seconds access time it will be more efficient to just assign the value and not to check if it's already assigned to the variable.
    It is not the access time which matters, it is the difference between read time and write time. Only if the write time is greatly longer than the read time would it help to do a read first before writing. Otherwise, if the compiler doesn't throw away your code, it will be slower than if you left it alone!

    I prefer not to be dependent on compiler decisions, so in cases where I can help the compiler take decision, I do so.
    On the other hand, if you try to do things to help, without really understanding what is happening under the hood, you could do more harm than good.
    It is best to analyse any algorythms you are using for efficiency, and let the compiler optimise low level stuff. If you are really concerned about efficiency, look at the assembly code produced for the most often used routines, and if necessary, rewrite them in assembler if you can see something inefficient the compiler is doing.

Children
No data