Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Since For loops is n Timecomplexity So is it Better To Use Only couts for example in cpp and never use for loops?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading