This is my first try at arduino programming, and i'm also not extensively experienced in other languages so please bear with me
I'm making a (fan)controller for my pc with an Uno, and i'm running low on memory (especially for the variables.) I've used the smallest data type possible everywhere.
While looking for ways to optimize my script, the following came to my mind:
in most examples and scripts I see online, ppl assign names to the pins they use in order to keep things clearer. This action would increase the amount of variables used, and increase processing time because the microcontroller has to execute more commands.
Thus it would be more efficient to subsitute the variables with the actual pins that are used. So
const int ledPin = 9;
digitalWrite(ledpin, HIGH)
would become:
digitalWrite(9, HIGH)
Is this true or does the compiler optimize this anyway? I know comments and such are stripped but i'm not sure about those variable substitution. This is the most basic example but if it makes a significant difference in memory usage and/or runtime I can apply it to every case in my script.
The compiler will optimize your example, because ledPin is a constant, not a variable. Just test both and see the memory usages after compilation, they will be the same.
AWOL:
Does it really matter if your fan switches on at x time or x plus 5 microseconds?
In my case not really, but in other situations shorter runtime would be desirable I suppose.
If the gained time is in the order of microseconds it doesn't matter for me, but that was my very question. I didn't know whether it would make a significant difference or not. But it doesn't, it appears.
guix:
The compiler will optimize your example, because ledPin is a constant, not a variable.
Thanks, in my example I use const, but in my script there are plenty of cases where the vars are not constant. Will it be different for changing vars?
The optimising compiler is extraordinarily efficient at finding constant expressions and converting them into absolutely no code at all. Infinitely fast. Truly. If you want to know, write a test. I tested this example below before posting it here:
void setup() {
int x;
const int MAX_COUNT = 100;
Serial.begin(9600);
while(!Serial); //required for Arduino Micro
unsigned long start = millis();
for(x=0; x<MAX_COUNT; x++) {
//do nothing
}
Serial.print(start-millis());
Serial.println(" milliseconds duration.");
Serial.print("x = ");
Serial.print(x);
}
void loop() {}
Results:
0 milliseconds duration.
x = 100
It can count to 100, 1000, or 10000 in zero milliseconds. The compiler is very smart at finding these optimisations. What it can't do for you is understand what you want.
If you have a requirement like "I want to count up to a hundred" then the compiler can't understand that English sentence. So you write code which the compiler can understand. However, the most important entity reading that code is the programmer, not the compiler. If any programmer [including you 6 months from now] can't look at that code and quickly understand "I see he wanted to count up to a hundred" then your original code has failed and will be thrown away and rewritten. Even if the code still does what you want, it is a failure.
Robin2:
How could a fan controller program use more than a small fraction of an Unos' memory ?
Well it's actually more than a simple fan controller, among other things it also monitors multiple readings from my pc and sends info back. Next to that I like to write my scripts as efficient and neat as possible,(like everyone else? lol :p) even if I'd have far more than sufficient memory available. With those lil' arduino scripts I can train myself in this. I guess if I really run out of memory, I can send the info to pc and store the vars there..
My code is far from finished and I just started making a setup.
I could post it but it wouldn't benefit the topic.
MorganS:
My answer is a very big: NO DON'T DO THAT!
The optimising compiler is extraordinarily efficient at finding constant expressions and converting them into absolutely no code at all.
(...)
If any programmer [including you 6 months from now] can't look at that code and quickly understand "I see he wanted to count up to a hundred" then your original code has failed and will be thrown away and rewritten.
Okay so you mean to say; just write it so that you and other programmers understand what the purpose of the actual code is, the compiler will optimize it better than you ever could anyway?
I have extensively commented how the calculations are made and where they derive from, so it won't be a problem understanding it, and I will be the only person using and reading the script.
But I can see now it is of much greater importance if you have to write a script that other programmers have to read, too. Thanks for pointing that out
It's clear to me now that all those things work completely different at machine level, when the script is optimized by the compiler.
There are situations where you do need to 'optimise' by hand but that should only be done to 1% or less of your code and only when that 1% represents 99% of the actual running time of the code, in some inner loop that has a required timing performance. Then you write lots of comments around it explaining what it is supposed to do and why it looks like you've done something weird like using a boolean variable as an index in a loop.
ISRs (interrupt service routines) also sometimes need this kind of optimisation, say when you have a timer interrupt occurring 1000 times per second and you want it to use less than 50% of your CPU time.