Code speed - equation vs. array

I'm working on trying to optimize some code.

I have an array with 60 values that are the results of an equation. The results are fixed - they won't change.

As a general rule - is it going to be faster/more processor efficient to call the values/results from an array - or should I consider calling an equation each time I need to calculate one of the values?

The values aren't getting called more than a few times a second at most.

Just curious - thanks.

J

That depends on how complicated the code is to compute the value.

There is processing required to compute the array location and to retrieve the value from the array. This is generally addition and multiplication, which are fast operations. So is retrieving the value.

Computing the value each time it is needed is probably going to be less efficient.

The tradeoff, though, is storage is space.

I think arrays will be faster no matter what (unless equation is an optimized macro).

Thanks all,

I figured an array would be quicker - and I like the fact that I can see the values.

Much appreciated - cheers.

J

I think AlphaBeta is correct on this one - there is no way you will be able to write a function that will consume less bytes (for arithmetic/logic operations) than you produce.

Also, if you use fixed access to array locations (e.g. val[3]) there is no overhead related to array access compared to static memory locations. So it will always be much faster to precompile fixed values and include them as constants in your application.

A couple other points to consider:

  • Prefix the array definition with "const". If you do not, the compiler will allocate FLASH memory (for the initial values) plus RAM. There will also be code generated to move initial flash data to RAM. Constants however will only consume FLASH memory.
  • Use the smallest data type that can hold your values. For 8-bit CPU's like the AtMega series, there is significant overhead with declaring and using anything larger than 8-bits.

Constants however will only consume FLASH memory

I wrote a little test program and I can't find any difference whan "const" is included in the array definition.

Are you certain "const" values are not copied to SRAM?

Are you certain "const" values are not copied to SRAM?

I had to go back and read up on this topic and found that I'm probably wrong. According to the avr-libc user manual, the compiler will allocate RAM also for const data (and add initialization code to move data from FLASH to RAM on startup). To force data into FLASH only - we will have to use the PROGMEM attribute.

Someone may argue that const would still allow fo smaller code as the optimizer may choose to keep global constants in general purpose CPU registers only. My experience however tells me that the optimzer is pretty good irrespective of "clumsy" coding and would most likely figure out that variables are constant even though we didn't declare them as such. So the use of const may not add much value beyond documenting through declaration that the value will indeed remain constant for the lifetime of the application.

Reading the manual there was another point that intrigued me. The "C" language states by definition that "uninitialized" variables shall be set to zero. Consequently the following decalration will yield the same values for var1 and var2:

int var1;
int var2 = 0;

The AVR compiler however has chosen to treat these definitions differently. According to the manual both "var1" and "var2" are guaranteed to be initialized to zero, but var2 will allocate space in FLASH (to hold the initializer) whereas var1 will not. So there would be a few bytes to gain here for the size optimizer enthusiast.

So the use of const may not add much value beyond documenting through declaration that the value will indeed remain constant for the lifetime of the application.

Which makes it worth using.

The values aren’t getting called more than a few times a second at most.

If you only need the values a few times a second, the speed difference just doesn’t matter. Isn’t your code going to be clearer and more compact if you use the equation?

Incidentally, if you can contain all your math in efficient operations equation could be faster; i.e. if you bitshift/add/subtract byte variables.

If you get into performing floating point math an array has proven to be much faster. I recall removing non-power-of-2 divisors from an equation and replacing them with more efficient approximations gained me about 300 clock cycles for a function call. I think it was 4 or 5 separate instances.

Incidentally, if you can contain all your math in efficient operations equation could be faster

An array access with an index variable requires only three machine instructions (+1 for each byte in an array element). That’s going to be very difficult to beat with an equation.

If you get into performing floating point math an array has proven to be much faster

Dang right! :wink:

Thanks again everyone - this has been really helpful.

I'm going to stick with the array for now - seems like I might need to tweak a few of the values manually anyway, which would now mean running - and correcting - an equation if I wanted to get rid of the array.

Much appreciated.

J