Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 86
My wording of terms may not be accurate, but from my knowledge, the time complexity of an algorithm deals with counting the number of primitive operations involved in the algorithm, given the number of input elements processed. For instance, a program to find the max element in an array would require us to traverse the entire array. Let the number of elements in the array be n, the time complexity in Big-O notation would be O(n). In comparison, a program to compare each element in the array, to each element in the array, would have a time complexity of O(n²), and so on. http://bigocheatsheet.com While certain loops may arguably be faster (by a really small margin) than another, loops do not affect the number of primitive operations involved in an algorithm. I am pretty sure that the type of loop you use in a program does not affect the time complexity of your program, i.e. the type of loop you use has no direct relationship with the time complexity. However, if you are talking about execution time, e.g. what you get from benchmarking two programs using different types of loops on the same number of operations, some may be faster (as pointed out earlier), although the margin is, again, very small.
19th May 2018, 10:57 AM
Hatsy Rei
Hatsy Rei - avatar
+ 25
I think it depends a lot on the language and above all on the structure you want to iterate on. There are languages where the use of some of them is recommended (or of an iterator) because the performance is superior in specific cases.
19th May 2018, 10:56 AM
Mickel
Mickel - avatar
+ 23
Just a quick note; you should know what the language is doing well enough to time the algorithm and NOT the compiler / optimizer. Here's one example where having your variables in the wrong place can hurt your tests: https://code.sololearn.com/Wve32KP16EDd/?ref=app In c++ I believe variable placement also matters under compiler optimization modes; in some cases you get "automatic variables" in a register, in others your variable ends up on the stack.
20th May 2018, 2:28 AM
Kirk Schafer
Kirk Schafer - avatar
+ 14
Loops performance also depends on "what's going on" inside the loop body besides the iteration count, if you sub the task e.g. function calls rather than inline operation, or query information from a remote system (e.g. databases), or read from a slow storage media you may expect performance degradation despite which loop you employed.
21st May 2018, 12:55 PM
Ipang
+ 11
CHaran LEo25😎 , I've seen other similar tags and there's nothing wrong with them (like [RESOLVED]). It is only specifying that it is not a question, but a thread to comment on the subject (and can provide some useful information for others). Or at least that's my way of interpreting it.
20th May 2018, 4:27 PM
Mickel
Mickel - avatar
+ 9
When we talking about Time complexity then it depends on the so many internal factors and external factors & if we consider any loop for time complexity then it will take not more time .so, we can ignore this in time complexity . And about which loop is best then it is totally depends on your algorithm structure.
19th May 2018, 3:29 PM
Chappie
Chappie - avatar
+ 9
for, while, and do-while loops all are pretty much identical with one exception: do-while loops will always run the code inside at least once. Because do-while loops are used deliberately to do this, if you are using a do while loop, this should be because you need to run the inside code at least once, not just to have a different syntax. The loops don't really matter, what matters is the code inside the loops. While it is true that some styles of loops could be used more for slower types of algorithms, iteration by iteration what matters is the code running inside the loop.
19th May 2018, 7:53 PM
Tavi Kohn
Tavi Kohn - avatar
+ 9
I agree with what the most say, there is a small executional margin, but generally the complexity is the same. I made a small Python program where you can see the speed of different python functions and loop. On the Code Playground all might be 0s, I recommend running on QPython 3 or for Computer Python 3.x https://code.sololearn.com/ctGno9UXS3cv/?ref=app
24th May 2018, 4:47 AM
Martin Möhle
Martin Möhle - avatar
+ 8
Time complexity of algorithms does not depend on the programming language used or the specific type of loop used as long as those loops do the same thing and specifies only (worstcase) asymptotic behavior
19th May 2018, 11:29 PM
Max
Max - avatar
+ 7
Depends For single for loop it's O(n)✌
21st May 2018, 11:23 AM
Junth Basnet
+ 6
Suraj S j, it's better that you look inside the Q&A section, I'm sure you'll find some material that can help you. If you do not get what you want you can make a post about it. Writing questions outside the topic of a post is classified as SPAM, and may cause your comment to be removed. In addition, with the other methods you get more information.
24th May 2018, 3:20 AM
Mickel
Mickel - avatar
+ 5
speed: while > do-while > for for loop will check it's assignment part and will perform it in the 1st iteration , if there any and condition part is common in all three loops . the increment or decrement part of for loop will be checked and performed in every iteration after the 1st , if there any . in while and do-while loop assignment and increment / decrement parts are missing . so , they will perform slightly faster but won't be enough to change the overall complexity of an algorithm
23rd May 2018, 6:58 AM
Nieb Hasan
Nieb Hasan - avatar
+ 5
All loops are goto's. The for-loop is redundant (except in C++ with the : operator where it acts as "for a in b"). Example: for (int ii = 0 ; ii < 5 ; ii++) run_this(); VS int ii = 5; while (ii--) make_that(); VS label: do_this(); if (--ii) goto label; ...But it doesn't matter because of compiler optimization. If ii is known, it is unfolded.
24th May 2018, 12:20 AM
non
+ 5
All these loops are similar in the fact that the continue until they have satisfied something. well all of them work the same way, it just depends hiw it works. I kight say they all have the same complexity. For general Informatics Olympiad purposes you can treat them all to have O(N) complexity
24th May 2018, 1:00 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
for loop is faster
26th May 2018, 3:59 AM
deeyae
deeyae - avatar
+ 4
I would say a for since u already know how many times a loop will iterate. There is a way to clock the time it takes for the compile and execution of a loop condition.
22nd May 2018, 1:06 AM
Apple Blossom
Apple Blossom - avatar
+ 4
while loops and for loops have the same performance. There may be, however, special cases (like dynamic conditions and whatnot) that change which looping structure's performance is better. You really can't say that one looping structure is better than the other without checking to see what code is generated by the compiler.
22nd May 2018, 3:49 AM
Andrew Watts
Andrew Watts - avatar
+ 4
In fact, there's only one loop - while. You could write a for like this: int i = 0; while(i < x) { i++; //Do other stuff } Because what it looks like on a closer look (not really, but almost): 1 int i = 0; 2 3 i++; 4 //Do other stuff 5 IF i>x JUMP TO LINE 7 6 JUMP TO LINE 2 So with for-loops (and foreachs, you forgot these), you have whiles whith extra stuff. But in the end, it does not really matter what you use regarding performance - use the one most befitting your usecase.
24th May 2018, 11:13 PM
D B
D B - avatar
+ 3
Well, it doesn't matter how fast a type of loop is when the codes within it are like a ballast. The use of too many variables or itering over some nastily nested iterables makes it hard to keep up the speed, not with all the garbage collecting or memory allocation processes. And that does take some time. So if one must use all of the variables, then a plan B is in order. Recursion or threading could save precious time. Anyway, I think while is a little bit faster than the rest. That's my opinion, more or less. Cheers
24th May 2018, 6:48 AM
Osamah Mohammed Al-Haddad
Osamah Mohammed Al-Haddad - avatar
+ 3
Answering newly to your question, if you put the focus only in the loops I can say that it will be more efficient which takes pointers to adress of memory and uses simplest variables than one which had used arrays or complex variables. It will also depend on the machine and language which you use. At the end, it's depending on many factors.
24th May 2018, 3:52 PM
David Rueda 🇪🇸
David Rueda  🇪🇸 - avatar