c++ - Boolean condition evaluated inside for loop performance -


i have 2 loops. don't understand why first 1 runs faster second one. can explain possible?

first:

for (int x1=0;x1<1000;x1++){    for(int x2=0;x2<1000;x2++){       if(x1<500){          a[x2+x1*1000]=100;       }    } } 

second:

bool cond; (int x1=0; x1<1000; x1++) {     cond = x1<500;     for(int x2=0; x2<1000; x2++){         if(cond){             a[x2+x1*1000] = 100;         }     } } 

(i simplified code)

the simplest answer when use

if(x1<500){       a[x2+x1*1000]=100;           } 

you using direct method in values compared , work done when use

if(cond) {    a[x2+x1*1000] = 100;          } 

then first calculates answer of " cond " applies if condition , being more specific can take if cond function call , if(x1<500) simple comparison within main() . got answer , if want know else topic tell me :)


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -