L
lovecreatesbea...
Is case 2 better than case 1 in performance? For case 2 doesn't create
and destroy objects inside a loop again and again for 1000 times.
/*case 1: local definitions inside a loop*/
for (int i = 0; i != 1000; ++i){
int a1;
int a2;
/* more local variables */
int an;
/* more operations on variables: a1, a2, ..., an */
}
/*case 2: definitions outside a loop*/
int b1;
int b2;
/* more local variables */
int bn;
for (int i = 0; i != 1000; ++i){
/* more operations on variables: b1, b2, ..., bn */
}
and destroy objects inside a loop again and again for 1000 times.
/*case 1: local definitions inside a loop*/
for (int i = 0; i != 1000; ++i){
int a1;
int a2;
/* more local variables */
int an;
/* more operations on variables: a1, a2, ..., an */
}
/*case 2: definitions outside a loop*/
int b1;
int b2;
/* more local variables */
int bn;
for (int i = 0; i != 1000; ++i){
/* more operations on variables: b1, b2, ..., bn */
}