![]() |
2010-06-12
, 12:47
|
Posts: 51 |
Thanked: 32 times |
Joined on Jan 2010
|
#12
|
![]() |
2010-06-13
, 09:28
|
Posts: 3,319 |
Thanked: 5,610 times |
Joined on Aug 2008
@ Finland
|
#13
|
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".
![]() |
2010-06-13
, 13:53
|
Posts: 726 |
Thanked: 345 times |
Joined on Apr 2010
@ Sweden
|
#14
|
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.
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".