performance
in
Programming Questions
•
1 year ago
I've learned that it is better to not declare values inside a loop cause they will get recreated every iteration.
But when i do a test to figure out the difference then one is 143ms and the other 145ms.
So what's up with creating values inside a for loop, does the compiler outsmart the simple example below?
- void setup() {
- int start = millis();
- for (int i = 0; i < 185000000; i++) {
- float bezierLength = 6;
- float targetL = 7;
- float l = 0;
- float t1 = 2;
- float t2 = 3;
- float xPos1 = 4 ;
- float yPos1 = 5;
- float zPos1 = 6;
- float xPos2 = 7;
- float yPos2 = 8;
- float zPos2 = 9;
- int steps = 8;
- }
- println(millis()-start);
- }
- void setup() {
- int start = millis();
- float bezierLength = 6;
- float targetL = 7;
- float l = 0;
- float t1 = 2;
- float t2 = 3;
- float xPos1 = 4 ;
- float yPos1 = 5;
- float zPos1 = 6;
- float xPos2 = 7;
- float yPos2 = 8;
- float zPos2 = 9;
- int steps = 8;
- for (int i = 0; i < 185000000; i++) {
- bezierLength = 6;
- targetL = 7;
- l = 0;
- t1 = 2;
- t2 = 3;
- xPos1 = 4 ;
- yPos1 = 5;
- zPos1 = 6;
- xPos2 = 7;
- yPos2 = 8;
- zPos2 = 9;
- steps = 8;
- }
- println(millis()-start);
- }
2