Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'for' loop vs Qt's 'foreach' in C++

Which is better (or faster), a C++ for loop or the foreach operator provided by Qt? For example, the following condition

QList<QString> listofstrings; 

Which is better?

foreach(QString str, listofstrings) {     //code } 

or

int count = listofstrings.count(); QString str = QString(); for(int i=0;i<count;i++) {     str = listofstrings.at(i);     //Code } 
like image 772
Ajay Avatar asked Apr 21 '09 04:04

Ajay


People also ask

Which is better foreach or for loop?

This foreach loop is faster because the local variable that stores the value of the element in the array is faster to access than an element in the array. The forloop is faster than the foreach loop if the array must only be accessed once per iteration.

What is difference between for loop and foreach?

For Loops executes a block of code until an expression returns false while ForEach loop executed a block of code through the items in object collections. For loop can execute with object collections or without any object collections while ForEach loop can execute with object collections only.

Which is faster for or foreach in C++?

As it turned out, FOREACH is faster on arrays than FOR with length chasing. On list structures, FOREACH is slower than FOR. The code looks better when using FOREACH, and modern processors allow using it. However, if you need to highly optimize your codebase, it is better to use FOR.

Does C have a foreach loop?

There is no foreach in C. You can use a for loop to loop through the data but the length needs to be know or the data needs to be terminated by a know value (eg. null).

Is the Qt for loop better than the C++ for loop?

The foreach from Qt has a clearer syntax for the for loop IMHO, so it's better in that sense. Performance wise I doubt there's anything in it. You could consider using the BOOST_FOREACH instead, as it is a well thought out fancy for loop, and it's portable (and presumably will make it's way into C++ some day and is future proof too).

Can I write a foreach loop in Qt?

In summary, Qt's foreach can only be used for read-only loops and thus should be avoided. Qt will happily let you write a foreach loop which you think will update/modify your container but in the end all changes are thrown away. Show activity on this post. First, I completely agree with the answer that "it doesn't matter".

Which C++ language should I use for my Qt code?

New code should prefer C++11 range-based loops. The foreach keyword is a Qt-specific addition to the C++ language, and is implemented using the preprocessor. Its syntax is: foreach ( variable, container) statement.

Why does Qt take a copy when iterating?

Qt automatically takes a copy of the container when it enters a foreach loop. If you modify the container as you are iterating, that won't affect the loop. (If you do not modify the container, the copy still takes place, but thanks to implicit sharing copying a container is very fast.)


1 Answers

It really doesn't matter in most cases.

The large number of questions on StackOverflow regarding whether this method or that method is faster, belie the fact that, in the vast majority of cases, code spends most of its time sitting around waiting for users to do something.

If you are really concerned, profile it for yourself and act on what you find.

But I think you'll most likely find that only in the most intense data-processing-heavy work does this question matter. The difference may well be only a couple of seconds and even then, only when processing huge numbers of elements.

Get your code working first. Then get it working fast (and only if you find an actual performance issue).

Time spent optimising before you've finished the functionality and can properly profile, is mostly wasted time.

like image 110
4 revs, 2 users 97% Avatar answered Oct 18 '22 13:10

4 revs, 2 users 97%