These two loops for can be executed separately?

0

In terms of calculation speed, it is the same:

for(int a = 0; a<10; a++)
{
  var1[a] = random(255);
}

for(int b = 0; b<10; b++)
{
  var2[b] = random(255);
}

a:

for(int a = 0; a<10; a++)
{
  var1[a] = random(255);
  var2[a] = random(255);
}

Because from the ignorance and my common sense, despite being two cycles (hence, 20 searches in the arrays) in the first case, in the 2nd it changes its value to twice as many variables as in the first.

Thank you very much

    
asked by thru. 24.09.2017 в 23:21
source

1 answer

0

If your question is whether the runtime of both functions is the same, the answer is Yes. And it's O (n) .

As far as runtime is concerned, it is equal to traverse 2 arrays of length n in separate loops ( 2n ), which traverse the same 2 arrays, as long as no are nested, within the same loop.

.

    
answered by 25.09.2017 / 00:42
source