Public Group

# which is faster?

This topic is 4623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

for (int i = 0; i < myVec.size(); ++i)
myVec->DoSomething();

or
vector<myClass*>::iterator iObject;
for (iObject = myVec.begin(); iObject != myVec.end(); iObject++)
(*iObject)->DoSomething();

also, does anyone know if there's a faster way to render with gdi than using BitBlt() ?

##### Share on other sites
First of all, your post-increment in the second section could generate an unused temporary object, but all the big-name compilers will optimise that away. I've read that dereferencing a pointer can be costly in cache misses, but I'm going to again go with the compiler optimising that sort of thing away (in your very basic example).

##### Share on other sites
This is a case of "Premature Optimization"; the compiler will figure out the way that's the fastest for you here, and will probably compile down to something closer to the first one, even though the second one is generally preferred due to readability concerns.

edit: and yeah, like ^^ he said, you really need to pre-increment the iterator.

So, pick one and keep coding.

for_each

##### Share on other sites
Quote:
 Original post by zappernapperfor (int i = 0; i < myVec.size(); ++i) myVec->DoSomething();orvector::iterator iObject; for (iObject = myVec.begin(); iObject != myVec.end(); iObject++) (*iObject)->DoSomething();

for_each(myVec.begin(), myVec.end(), mem_fun(&myClass::DoSomething));

That's precisely the problem with premature optimization. You're so focused on making something fast that you don't check to see if you could make it better first - more compact, easier to understand, etc.

##### Share on other sites
std::for_each(myVec.begin(), myVec.end(), boost::bind(&myClass::DoSomething, _1));

Edit:Heh, thought i might be beaten.

##### Share on other sites
the example w/ the iterator is something i got out of a book. i prefer to use the for loop and increment a counter b/c that looks cleaner than creating an iterator. i always wondered how specifically to use for_each, i think i will try that, so thanx!

Edit: it just goes to show you, there's always SOMETHING in the STL that you've never seen b4 (mem_fun()), oh and i didn't think i was being premature, i have the game all written and was running my profiler, and wanted to tweak the time spent rendering

##### Share on other sites
Quote:
 Original post by zappernapperoh and i didn't think i was being premature, i have the game all written and was running my profiler, and wanted to tweak the time spent rendering

Ok, it weren't premature just stupid. Also I think you should change main(char** argv,int argc) to main() because you need your program to be faster. If you have profiled and you are sure that specific loop is the problem, just TRY changing it run it through the profiler and look how you have gained 1-5 cycles. If such a loop is actually your bottleneck I'm sure you should optimize DoSomething instead of the loop itself. This is like changing 1.0f to 1.0F to get a better performance.

##### Share on other sites
Quote:
 Original post by CTarOk, it weren't premature just stupid.

Hey! I do the irrational and indiscriminate insulting around here, got that?! Now apologize to the gentleman!

##### Share on other sites
Quote:
Original post by Oluseyi
Quote:
 Original post by CTarOk, it weren't premature just stupid.

Hey! I do the irrational and indiscriminate insulting around here, got that?! Now apologize to the gentleman!

I don't like your face. [grin]

[Edited by - Rebooted on January 29, 2006 8:10:14 AM]

1. 1
2. 2
3. 3
4. 4
Rutin
11
5. 5

• 12
• 19
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632665
• Total Posts
3007712
• ### Who's Online (See full list)

There are no registered users currently online

×