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
Hi, ...if the variable already has this value What do you mean? Next construction: if (a != 9) a=9; has no sense with the exception of writting into cycle-limited areas (for example, EEPROM, FLASH etc). Btw, another way to assign a value to a variable is make it via array; something like it: static unsigned char array[256] = {value1, value2, ...}; a = array[x]; such way should work fast if compiler translates it in MOVC A,@A+DPTR - just one assembly command. Good days! Good days!
Oops, has no sense here I mean that insead if (a != 9) a = 9; you may just write a=9; Good days!
Is it more efficiency to just place the value or first to check if the variable already has this value? Almost certainly the former. The optimizer will remove the check as redundant and just put in the write operation, in which case the two choices would be effectively the same thing. Otherwise, checking and then writing will always be slower, simply because it has more operations to be performed. About the only case where checking first could win you anything would be with unusual types of memory where writing something takes considerably longer than reading it, but such memory would hardly ever be mapped into normal address space as writable, to begin with.
Thanks a lot guys for your help. 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. I prefer not to be dependent on compiler decisions, so in cases where I can help the compiler take decision, I do so. Have a nice day all, Ruven.
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.