View Single Post
Posts: 3,319 | Thanked: 5,610 times | Joined on Aug 2008 @ Finland
#13
Originally Posted by Joorin View Post
Faster? As in "Faster to write" or something else?

I agree on this construction being less error prone but faster is not something I'd connect to it.

The "foreach" construction is syntactical sugar, as far as I know, and as fast, or slower, than regular indexing. With a normal compiler, I'd guess "just as fast" but not "faster".
Yes, it’s a macro construct/Qt extension. As for speed, that’s a bit more complex topic. Faster to code, certainly, but not only that. If you do a plain sequential benchmark, it *might* come out slower, but then we’re getting into deep water - by default foreach provides you with safety by making a copy of the container, so if you meddle with the content, you don’t trip over yourself. Now, if you’re NOT altering it, you can do foreach (const type &, container), which is roughly on par time-wise. Exact timings will of course depend on particular compilers. But at that point, you already have a flow that is very easy to convert to QtConcurrent::map, which (at least on the desktop, but soon on mobiles, too) unleashes all those sleeping cores and blows a simple ’for’ out of the water.
__________________
Blogging about mobile linux - The Penguin Moves!
Maintainer of PyQt (see introduction and docs), AppWatch, QuickBrownFox, etc