Want Faster Code?
Be Careful!
|
||||||||||
|
|
||||||||||
|
Want Faster Code? Be Careful! Often, when you write a program, you want to make sure you have written efficient code. But efficiency, like beauty, is in the eye of the beholder. Heres an example function f(), which returns a value from a lookup table. There are two versions. Lets see which is more efficient. First, lets make an array of constants:
const unsigned int array1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; return array1[arg]; } Heres the same function, this time written with a static array instead of a const unsigned int f (unsigned int arg) { static unsigned int array1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; return array1[arg]; } Which one is more efficient? You might think that the const version is, and you would not be alone. After all, this code tells the compiler that you are definitely not going to change the contents of the array. That way, it can write more efficient code, right? Lets look at the resulting assembler code, that I created using gcc on an x86 system. Heres the const version: _f: and the static version: _array1.0: Which one is more efficient? The const version uses ten movl instructions to set up the const array; the static version adds push/pop instructions but does the array lookup with a single movl instruction. The program will execute more instructions for each call of f() if the lookup table is defined as const instead of static. And it will use twice as much memory because the actual values are included in the instructions and are also stored in memory when the instructions get executed. In the static version of the program, the constants are set up at compile time and can be directly referenced in memory. This saves space and time. This kind of idea is used wherever a lookup table is needed. A lookup table that has many values (such as for trigonometric functions) will take a lot longer to execute if you declare the table const vs. static. In a real-time embedded system, with a slow processor and limited memory, you might not have the luxury of doing that. Lets think about why the compiler does things this way. Is there any benefit to the code in the const example? Certainly, there is. The const code never places the unchangeable data where external code can modify it. The extra instructions make sure that the correct constants are loaded on every call of f(). For data integrity, these are good things. On a complex project, it can be more efficient, in terms of debugging time, to add that extra level of protection. But if you are working on a small embedded system, you might not want that protection. It comes at a price in both memory consumption and execution time. In this case, the static code is more efficient, in terms of your scarce CPU resources. Which way you go depends on what you want to accomplish.
Please note: Any trademarks and trade names of others mentioned in this message are the property of their owners, and not Stoney Hill Associates, LLC. We respect the intellectual property of others. The information provided is believed to be reliable, but we cannot guarantee that the procedures and information given here will work correctly for your specific situation.
If you would like help with a computer or software problem you face, contact us. Send an email to request@stoneyhillassociates.com.
Want to subscribe to this newsletter? Just join our mailing list:
|
||||||||||
© 2006 Stoney Hill Associates, LLC |