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

前端 未结 11 847
我寻月下人不归
我寻月下人不归 2020-12-14 05:28

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

QList

        
相关标签:
11条回答
  • 2020-12-14 06:03

    I would expect foreach to work nominally faster in some cases, and the about same in others, except in cases where the items are an actual array in which case the performace difference is negligible.

    If it is implemented on top of an enumerator, it may be more efficient than a straight indexing, depending on implementation. It's unlikely to be less efficient. For example, if someone exposed a balanced tree as both indexable and enumerable, then foreach will be decently faster. This is because each index will have to independently find the referenced item, while an enumerator has the context of the current node to more efficiently navigate to the next ont.

    If you have an actual array, then it depends on the implementation of the language and class whether foreach will be faster for the same as for.

    If indexing is a literal memory offset(such as C++), then for should be marginally faster since you're avoiding a function call. If indexing is an indirection just like a call, then it should be the same.

    All that being said... I find it hard to find a case for generalization here. This is the last sort of optimization you should be looking for, even if there is a performance problem in your application. If you have a performance problem that can be solved by changing how you iterate, you don't really have a performance problem. You have a BUG, because someone wrote either a really crappy iterator, or a really crappy indexer.

    0 讨论(0)
  • 2020-12-14 06:06

    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.

    0 讨论(0)
  • 2020-12-14 06:07

    First off, I'd just like to say I agree with Pax, and that the speed probably doesn't enter into it. foreach wins hands down based on readability, and that's enough in 98% of cases.

    But of course the Qt guys have looked into it and actually done some profiling: http://blog.qt.io/blog/2009/01/23/iterating-efficiently/

    The main lesson to take away from that is: use const references in read only loops as it avoids the creation of temporary instances. It also make the purpose of the loop more explicit, regardless of the looping method you use.

    0 讨论(0)
  • 2020-12-14 06:07

    A benchmark, and its results, on this can be found at http://richelbilderbeek.nl/CppExerciseAddOneAnswer.htm

    IMHO (and many others here) it (that is speed) does not matter.

    But feel free to draw your own conclusions.

    0 讨论(0)
  • 2020-12-14 06:07

    For small collections, it should matter and foreach tends to be clearer.

    However, for larger collections, for will begin to beat foreach at some point. (assuming that the 'at()' operator is efficient.

    If this is really important (and I'm assuming it is since you are asking) then the best thing to do is measure it. A profiler should do the trick, or you could build a test version with some instrumentation.

    0 讨论(0)
  • 2020-12-14 06:09

    You might look at the STL's for_each function. I don't know whether it will be faster than the two options you present, but it is more standardized than the Qt foreach and avoids some of the problems that you may run into with a regular for loop (namely out of bounds indexing and difficulties with translating the loop to a different data structure).

    0 讨论(0)
提交回复
热议问题