6

I'm in a game programming school and here we have to learn about code speed, something that seems important.

Are there any tutorial or list of things to be aware of when programming in C/C++ ?

I wonder about many things, like why the default behavior of C is by passing data rather than reference/address, or how the compiler translate a reference to the assembler, or how a C loop translates itself to JMP's.

I'm concerned about that because python uses another way, but on the other hand python doesn't use an operator to copy the value, rather a function which can be syntactically heavy.

I don't really think knowing how to program in assembly is really is necessary, since it's painful, I guess it's just required to know about a register etc.

jokoon
  • 6,207
  • 11
  • 48
  • 85
  • 1
    Ultimately this question comes down to "Are there any tutorial or list of things to be aware of when programming in C/C++ ?" For that, I'd point you here: [http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list] – John Dibling Nov 18 '10 at 16:41
  • 1
    Obligatory "premature optimization is evil" comment here. If your algorithms are `O(too much)` (e.g. in a tile-based game, looping over all objects in the game to find out if there is an X right next to the player), then deep knowledge of C or Assembly cannot possibly save you. Optimize your own optimization efforts - find out where optimizing makes sense at all (by profiling, not by guessing), and "optimize" with thought. Also, propably "not a real question". –  Nov 18 '10 at 16:46
  • You need to know assembly, so you understand how a computer really works, so you can optimize for it. – Paul Nathan Nov 18 '10 at 16:53
  • I think it is a good thing to know what your compiler do. For me the question is relevant. I would recommend to try to figure it out by yourself (using the `-S` option of gcc for instance). Assuming you have a good general understanding of how a computer work (if not, ask your school). – log0 Nov 18 '10 at 16:54
  • 1
    This was a reasonable question, exemplified by the fact that it is oh so common. If there's any reason to close it, then there's a duplicate out there somewhere that someone should have found. Closing it for not being a real question is just ridiculous...but that's pretty common for SO. – Edward Strange Nov 18 '10 at 16:54

1 Answers1

6

Look up your compiler's documentation for the switch to output the asm step rather than machine code. Every compiler I've worked with has one. Use it to send some simple code to asm and examine it.

VS also has the option to 'view assembly' as you step through code.

There's not going to be any clear answer to your question. It's all dependent on the compiler and the architecture. Even on the arguments supplied to the compiler.

Edit:

The fact that there's no clear answer to your question won't stop people from making things up. One example is silly things like passing a large object (like a string :p) by reference instead of returning by value. These things will seem reasonable to a new C++ developer. OF COURSE passing a string into a function to get filled is faster than building a copy and returning it from that function only to copy it again in assignment. Fact of the matter is that it doesn't work that way though; it's very compiler dependent but you could be stomping all over your compiler's ability to optimize by trying to get all smart.

There's TONS of misinformation out there about making your code fast. Take it all with a grain of salt. Most people who claim to know how to make fast code are full of crap. The only reasonable answer to fast code is to profile your code. When it comes time to micro-optimize you'll be doing a bunch of stuff that's very specific to your target platform. General knowledge, "this is faster," stuff won't be of any use to you at that point.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
  • So I guess I have to explore higher level algorithms and design pattern to come up with a faster solution for each situation. Though I wonder how an engine like quake3 is that much fast. – jokoon Nov 18 '10 at 19:26