Hello All, Just starting out in C++. Why is my code so slow? The nested loops below I think would take a day to complete while the same code in Java completes in a few seconds. I tried it in CodeBlocks using different variants of C++. where I am wrong?
int a,b,c,x=0;
for (a = 0; a < 32768; a++) {
for (b = 0; b < 32768; b++) {
for (c = 0; c < 32768; c++) {
//x++;
}
}
printf("a=%d\n", a);
}
printf("x=%d\n",x);
Any C++ Tutroial Suggestions to build more strong skills.
Arjunkumar09:
What is Wrong?
You not using code tags...
You not providing a decent topic title...
You using code block instead of a real Arduino...
You using printf() where most Arduino's don't support it...
int a,b,c,x=0;
for (a = 0; a < 32768; a++) {
for (b = 0; b < 32768; b++) {
for (c = 0; c < 32768; c++) {
//x++;
}
}
printf("a=%d\n", a);
}
printf("x=%d\n",x);
For 8 bit Arduinos (or any system with sizeof(int) == 2) none of the loops terminates.
The biggest positive int16_t is 0x7FFF which equals 32767, so all int16_ts are always less than 32768.
Whandall:
For 8 bit Arduinos (or any system with sizeof(int) == 2) none of the loops terminates.
The biggest positive int16_t is 0x7FFF wich equals 32767, so all int16_ts are always less than 32768.
so true, just a quick test would reveal thatunsigned int a,b,c,x=0;