Does the speed vary depending on which of the overlapping for statements is repeated first?

Asked 2 years ago, Updated 2 years ago, 175 views

for(i=0 ; i<100; i++)
    for(j=0; j<1000; j++)
        printf("a");

And

for(i=0 ; i<1000; i++)
    for(j=0; j<100; j++)
        printf("a");

Both loops 100,000 times

I heard one of the two spins faster

Please explain why

c loops

2022-09-22 22:02

1 Answers

Here you can try to run the code yourself. Click the Run button at the bottom of the following code to compare the run times. There's no difference.

#include <stdio.h>
int main(){
    for(int i=0 ; i<100000; i++)
        for(int j=0; j<100; j++)
    {
            int c = 0;
    }
  return 0;
}
#include <stdio.h>
int main(){
    for(int i=0 ; i<100; i++)
        for(int j=0; j<100000; j++)
    {
            int c = 0;
    }
  return 0;
}


2022-09-22 22:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.