+ 1

for(;i<n;i++) or for(;i<n;++i) ?

The second form of the loop performs better. // For every i++ operation: temp = i+1; // in next statement i = temp; // happens. // For every ++i operation: i += 1; // happens. The difference itself is very unnoticeable for general use, but when running loops for large datasets or making real-time applications this difference counts. Prefer ++i. /* it's not a question, it's a post. new to the platform so posted it likewise */

19th Feb 2017, 5:43 PM
Sizr
Sizr - avatar
4 odpowiedzi
+ 1
my point was that ++i should be preferred over i++. and for loop was used for illustration as it is the most frequent place of use where this operation can make lot of difference.
19th Feb 2017, 6:21 PM
Sizr
Sizr - avatar
0
its all the same as i think so!!! but can any one tell me the diffrence clearly please?? thank you
19th Feb 2017, 7:45 PM
EOnour
EOnour - avatar
0
For integers, there is no difference between pre- and post-increment. If i is an object of a non-trivial class, then ++i is generally preferred, because the object is modified and then evaluated, whereas i++ modifies after evaluation, so requires a copy to be made. please refer http://stackoverflow.com/questions/4261708/i-or-i-in-for-loops for more answers n more clarification. if still in doubt, will be happy to help
19th Feb 2017, 7:54 PM
Sizr
Sizr - avatar
0
added a code to see the time difference https://code.sololearn.com/cJXYpDDaH3Y9/ run multiple times to see the average time difference, and ++i mostly outrun i++. as mentioned earlier for int it is not much difference. my o/p mostly was: 0.75 for i++ 0.73 for ++i
20th Feb 2017, 3:14 AM
Sizr
Sizr - avatar