I have seen using For loop in these two fashions. Is there any positive aspect of using !
instead of using <
? Which one is faster?
for (i = 0; i != imax; ++i)
{
}
or
for (i = 0; i < imax; ++i)
{
}
I have seen using For loop in these two fashions. Is there any positive aspect of using !
instead of using <
? Which one is faster?
for (i = 0; i != imax; ++i)
{
}
or
for (i = 0; i < imax; ++i)
{
}
Is there any positive aspect of using ! instead of using < ?
It is highly unlikely for this case. For the given example it's more semantic to use the latter because it expresses your real intentions.
Which one is faster?
You should never write your code with that in mind. Do it right, then do it fast. The better designed and maintainable your code originally is - the easier it will be to optimize it (if it ever will need to be optimized).
Which one is faster?
To answer this question, we need to go to lower level of programming and even to the machine level as it cannot be answered through higher level of programming with technical explanation.
First of all, lets see how assembly language performs loops. Before I start, I want to tell that I have used many sources and actual implemnetation of Assembly language, Architecture of Flags, Processor, etc vary. I don't want to write so much of assembly code so I am adding screenshots from http://www.cs.colostate.edu/~fsieker/misc/CtoLC3.html
In image given below, you can see how conditional logical expression are converted into numeric expression. Similar thing happens for Loops. LHS of numeric expression is what performed at machine level by an ALU (Arithmetic Logical Unit) of processor. Depending upon result of LHS i.e. (a-b), conditional jumps (branching) is performed
Implementation of loops is given below:
For all conditions value of expression (a-b) is calculated by processor's ALU. ALU never performs subtraction, it can only do addition. It simply adds the two's complement of number that is being subtracted to the first number.
For example, when 'a' & 'b' are fed to ALU, it adds 'a' with 2's compliment of 'b' and gives output. Along with the result it raises corresponding 'Flags' as shown below (ref. https://courses.engr.illinois.edu/ece390/books/labmanual/assembly.html )
Depending upon the values of various flag values, branching is performed. Example is shown below:
You can see no matter what sign one is using, whether '!=', '>', '>=', processor always performs '(a-b)'. Depending upon result 'Flags', 'Program Counter' gets incremented using 'Digital Logic' to fetch next instruction, which can either be inside the loop body or outside the loop body depending upon conditions. These two sources can give you little bit info about machine level implementation (however, its not necessary but only if you want do dive further).
http://minnie.tuhs.org/CompArch/Tutes/week02.html
http://people.tamu.edu/~akshitdayal/468/MIPS-Implementation.pdf
So, it does not makes it fast. However, second implementation 'i < imax' is correct if logical condition to be met is 'i less than imax' and 'i != imax' is correct if logical condition is 'i not equal to imax'. In your case its not affecting, but suppose, if increment is not ++i or i++ but i = i + 2 and condition is 'i less than imax', then there are chances that it becomes an infinite loop if 'i != imax' is used. Coding should always be the manifestation of basic logical reasoning behind it. :)
None of them are actually "faster" . It depends on the value of "imax " .For example ,in your first code , if "imax" doesn't equal zero (less than zero) , the 'for' loop will never stop
Unlikely there is any measurable difference in execution speed.
But there is significant difference in speed of reading such piece of code. You wrotefor
loop in unusual way which indicates to reader that there is something special about this particular loop. It make reading of such code more time consuming (need to understand what is the reason behind special version of code, if any) and as result making it harder to improve.
Usual way of writing for
loop in C#:
for (i = 0; i < imax; i++)
{
....
}
Pretty much any other way of writing the loop should cause you as developer stop and spend extra time to read and understand why original writer did something special for that particular loop.
Note: "usual" may vary between projects, so if code you use consistently happen to use other style like Yoda notation align your code with the rest go ahead and use ++i
or even imax > i
as long as it matches style of the rest of the code.
Note 2: My post turned out to be exact copy of https://softwareengineering.stackexchange.com/questions/70996/versus-as-condition-in-a-for-loop which covers C++ reasoning in great detail. Fortunately C# have native iteration with foreach
and there is no need to be concerned with "code will look the same between integer and iterator indexing".
Unfortunately there is no advantage in speed. However you could argue back and forth for which looks better, or is safer; especially if someone comes along and adds an extra ++i in the middle of the for loop in 6 months.
for (i = 0; i != imax; ++i)
Is prone to bugs introduced by programmers not paying enough attention. Most people are used to see this version around:
for (i = 0; i < imax; ++i)
and as such, they will probably parse it quickly and say "yup, that's a for
from 0 to imax - 1".
Having the thought that i
will stay inside [0, imax)
in mind, they might just write a quick hack to solve a hypothetical problem, basically changing the value of i
inside the loop. Then they notice that in some cases i
becomes greater or equal to imax
and the loop keeps on going.
As for speed, no, that should not be a factor to consider.