Creating variables at runtime or not?

Hello,

Does it make a difference (in speed) if a variable ia created at runtime or on top of a sketch? I want to optimize a library foor speed at the moment and was curious what the best and fastest way is :slight_smile:

Cheers.

What do you mean by 'creating a variable at runtime'?

Look at the assembly and see for yourself -

Or post your code, there are normally higher level optimisations that have a bigger effect before you need to get down to individual variables.

Duane B

OpenSource:
Hello,

Does it make a difference (in speed) if a variable ia created at runtime or on top of a sketch? I want to optimize a library foor speed at the moment and was curious what the best and fastest way is :slight_smile:

Cheers.

Which? The best or the fastest? - they are rarely the same.

OpenSource:
Does it make a difference (in speed) if a variable ia created at runtime or on top of a sketch?

Variables "on top of a sketch" are created at runtime. Your question doesn't make a lot of sense. Perhaps if you post the code in question and ask if it can be optimized for speed.

Put it another way, when or where the variable is created is not the issue. How many times you access it, and in what way, and what sort of variable it is, are far more likely to be important.

OpenSource:
Does it make a difference (in speed) if a variable ia created at runtime or on top of a sketch?

I guess that you may be asking about the performance of global versus local variables, but that's only a guess.

In which case local - inside a function is usually faster than global - outside any function.

Some other tips here - http://www.atmel.com/Images/doc8453.pdf

The reason that local variables are faster to work with is that global variables need to be read from memory and stored back into memory where as local variables only exist within the function so can be created in a register, used and then forgotten about once the function exits.

The effect is more noticable with bigger variables, a global long requires four reads (1 for each byte), four additions and four stores just to do this a++

A global unsigned char would bring this down to one read, one addition and one store

A local unsigned char could be as little as just one addition with no load or store instructions required - 12 times faster than the global long, three times fast than the global char.

Its never that simple in real life but thats the general idea.

Duane B

rcarduino.blogspot.com