Archived

This topic is now archived and is closed to further replies.

Where can I find tips on code optomization?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was just wondering if anyone knows were I can find some info on optomizing C/C++ code. I''ve heard things like "ordering of instructions affects speed b/c of the p4 pipeline architecture". Where can I find out more about these code optomizations? Also, is there some kind of ''code counter'' in VC++ 6.0. Basically what I mean is a utility that tells you how many clock cycles it will take to execute a section of code. Thanks for any info you can share. -Q "Take the red pill, and I show you how far the rabbit hole goes." -Morpheus (The Matrix)

Share this post


Link to post
Share on other sites
There are three rules to optimization:

1) Don''t optimize.
2) Don''t optimize yet.
3) Don''t optimize anything unless a profiler tells you to do so.

I suggest TrueTime for a profiler--once you get to step 3.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Well, that''s not the answer he was looking for. I got the impression that he was learning how to optimize. What is he going to do at step 3 if he never learned it huh?

Share this post


Link to post
Share on other sites
There is a profiler built into Visual C++ 6.0, if you have the professional edition (I haven''t used it, but it''s there; I''m sure the help docs have some more information). That said, listen to Stoffel. Finally, unless you''re very good, micro-optimisation will earn you very small gains (and let''s not even get started on the majority of the people who try to use inline assembly for optimisation ...). The best form of optimisation is macro-optimisation: Coming up with better algorithms.

Share this post


Link to post
Share on other sites
Assuming you get to step 3, follow these steps:

1. Don''t even touch assembly yet.
2. Look for a better algorithm, is Bubble Sort really the fastest way to sort that array? A better alogoritm can make your code run *exponentially* faster (read: a heck of a lot faster).
3. Improve your implementation using high level programming. Can you use a lookup table? Are you redoing calculations? Is that really the most efficent data structure/data type? A better implementation is where the bulk of your "optimizing" effort will and should be spent. Speed increases are in the order of 50%-5000+% (unless you did a bad job coding it in the first place, in which case 500,000% wouldn''t be unusual).
4. Are you sure you''ve done steps 2 and 3 as far as you can?
5. Check one more time.
6 [Optional]. Optimize key parts in assembly. People report up to 40% speed increases, though if your implementation is already well optimized that will fall to around 4% unless your algorithm can signifigantly benefit from instructions not used by your compiler (3D Now!, SSE, ect). Unless you have extensive experience with assembler (read: you haven''t just learned it, you have been practicing it for at least a year) and know the processor you are targeting inside and out you will more likely *decrease* the speed of your code by rewriting it in assembly. Remember, assembly is *not* a magic optimizing bullet.

Share this post


Link to post
Share on other sites
Thanks for the info. I don''t know what a profiler is or what it does, but I''m assuming it has something to do with optomization so I''ll check that out in the compiler documentation.

It''s a relief to hear that I won''t have to mess around with a whole lot of assembly. I''ll leave the assembly coding for Ti-8x games.

As far as looking for better algorithms, looks like I might need to take another computer science class...

-Q

Share this post


Link to post
Share on other sites
quote:
Original post by Qa303asmGuru
I was just wondering if anyone knows were I can find some info on optomizing C/C++ code. I''ve heard things like "ordering of instructions affects speed b/c of the p4 pipeline architecture".

Neither C nor C++ have anything to do with P4 pipeline architecture. IOW, optimising C and C++ and optimising for P4 are orthogonal concepts.
quote:
Original post by Michalson
3. Improve your implementation using high level programming.

This is good advice, and it''s so simple that I think people keep missing the point. I''ve seen architectures where people are willing to optimise by rearranging a function or providing caching on various data stores, where they''d get much better benefit by looking at the high-level picture. These same architectures would all have benefitted from reworking to eliminate various functions calls, and recomputation of values. An interesting one is the Law Of Demeter (which is really a law of encapsulation). Not only is it good from a design point of view, but violating LoD can actually result in unnecessary recomputations being carried out. That leads to people spending effort on optimising the accidental complexity they''ve needlessly introduced.

Share this post


Link to post
Share on other sites
quote:
Original post by Qa303asmGuru
Thanks for the info. I don''t know what a profiler is or what it does, but I''m assuming it has something to do with optomization so I''ll check that out in the compiler documentation.



A profiler is an extremely useful program that will track your program as it is running, recording when functions are called and how long it took them to execute. In this way it can present you with a report that could say something like "25% of the programs time was spent in function FooBar", at which point you know that FooBar is obviously a good target for optimization (unless of cource your program only has 2 functions, in which case you might want to optimize the other one taking 75% of the processing power)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by SabreMan
Neither C nor C++ have anything to do with P4 pipeline architecture. IOW, optimising C and C++ and optimising for P4 are orthogonal concepts.
[quote]I''ve seen architectures where people are willing to optimise by rearranging a function or providing caching on various data stores, where they''d get much better benefit by looking at the high-level picture. These same architectures would all have benefitted from reworking to eliminate various functions calls, and recomputation of values. An interesting one is the Law Of Demeter (which is really a law of encapsulation). Not only is it good from a design point of view, but violating LoD can actually result in unnecessary recomputations being carried out. That leads to people spending effort on optimising the accidental complexity they''ve needlessly introduced.

Huh?

You totally lost me dude.

----------------------------------------------------------------

Thanks for the info about the profiler Michalson. I''ll definetly check that out.

-Q

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
You totally lost me dude.

OK, I''ll put it another way. Basically, people can spend a lot of time optimising low-level code because their high-level design is inappropriate and causes the low-level code to be executed more times than it needs to. You should look at the high-level picture before deciding that low-level optimisation is appropriate.

Share this post


Link to post
Share on other sites
Ahhh, that makes sense now. Thanks for clearing that up for me. I''m thinking about purchasing the book "Code Complete" for some instruction on how to write structurally good high level code. That book recieved some very good reviews.

-Q

Share this post


Link to post
Share on other sites
quote:
Original post by Qa303asmGuru
VC++ 6.0 profiler ROCKS!!!!

As profilers go, it''s rather basic. There are far better ones out there if you are willing to fork out the cash.



God puts an apple tree in the middle of the Garden of Eden and says, do what you like guys, oh, but don''t eat the apple. Surprise surprise, they eat it and he leaps out from behind a bush shouting "Gotcha." It wouldn''t have made any difference if they hadn''t eaten it... because if you''re dealing with somebody who has the sort of mentality which likes leaving hats on the pavement with bricks under them you know perfectly well they won''t give up. They''ll get you in the end. -- Douglas Adams

Share this post


Link to post
Share on other sites
I tried using the VC++ 6.0 profiler, but it doesn''t seem to be working right. It starts my program, switches the screen mode, but after a few seconds, it switches back without any errors or anything. This is similar to what would happen if I have a problem with a null pointer or file not found or something, but when I run the program normally, I don''t get any problems at all. The profiling information is listed, but it''s only 7 seconds worth, really just init functions.

Anyone else had this kind of problem?

£§

Share this post


Link to post
Share on other sites
I think i will ask now something also about optimation.

I have heared of STLPort - a faster STL implantation. How fast is it realy? Does make using STLPort instead of the VC++ 6.0 stl sense?
Then i am thinking that a very good point for optimations are the basic mathstuff like vectors, matrices or qautern. I found at the webpage of AMD and Intel some optimiced routines and a peace of source wich detact the cup of the target computer. What do you think about this optinon, to use, if possible, special cpu optimiced stuff.

Thanks, Christian

Share this post


Link to post
Share on other sites