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

前端 未结 11 863
我寻月下人不归
我寻月下人不归 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: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.

提交回复
热议问题