   Where can I find tips on code optomization?
  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)  
  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.  ;  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?  ;  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.  ;  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. ;  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  ;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.  ;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)  ;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.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;  That last post was by me: Qa303asmGuru and not an AP.  I forgot to put in my pw...-Q  