Home > other >  Since For loops is n Timecomplexity So is it Better To Use Only couts for example in cpp and never u
Since For loops is n Timecomplexity So is it Better To Use Only couts for example in cpp and never u

Time:10-18

in algorithms Complexity For loops is N Time complex Nested For loop Is n2 time complex but cout in cpp or printf in c and cpp is Constant time Complex So its faster so is it Better To use Cout 10 times to print number1to10 since its Actually faster ? or ? (We should use Only for loops when Its really hard to code it for example 1k line of code or something!);

like

cout<<1;
cout<<2;
cout<<3;
cout<<4;

and so on Instead of

for(int i=1;i<=10;i  ){
cout<<i<<" ";
}

its a Beginner Question(so sorry if its weird) but it just got into my Head and i literally couldn't find any answer to it And tried to search for it But i found nothing!

CodePudding user response:

If you copy and paste a printout n times then the code still takes O(n) time. Unrolling the loop doesn't change the fact that you've got n printouts. Except now you have O(n) lines of code instead of the O(1) lines of a for loop.

  •  Tags:  
  • c
  • Related