6 ms·
How I doubled the speed of my game by giving up on C++
- andreasgonewild 9y agoLanguages are tool-boxes, the whole point of the exercise is to use the best tools for the job. People who tell you otherwise, who insist on this language being written exactly like that; may be safely ignored. I would guess that my C++ style breaks most rules in most books by now, the thing to realize is that there are no rules.
- ordu 9y agoThats the reason why assembly should be the first or the second language studied by beginner programmer. Thats the reason why Knuth use assembly in his TAOCP in code examples. Moreover if one wants to write fast programs he should be friend with -S compiler flag to know exactly what assembly compiler generates on this or that high-level code. When you are familiar with asm you see that `obj->virt_func()` not just fancy and convenient but also there are some magic behind that which cannot be described by simple `call mangled_virt_func_addr`. When you are familiar with asm you can see than calling function like int foo(int a, int b) { return a + b; } is way slower than just do `a+b`, and not just because of call/ret instuctions and possible cache misses, but because of compiler lacks ability to make some optimisations. And you write `inline` functions for such small tasks. When you are familiar with asm you inevitable see all this quirks. Sometimes you do not want to see them, but nevertheless you notice them every time, and you need to make a consious decision to leave some code inefficient, due to (for example) "i'm writing program that will be run one or two times, and time spent to make it fast would be greater than time saved on faster program runs".
- virtualized 9y agoIf I write the foo function in assembler, it will also be slower than just a+b. The assembler won't be able to inline the function, which makes the C version possibly more efficient. You are comparing apples and oranges.
- ordu 9y agoYes, I agree. But I'm not trying to persuade anyone to use assembly instead of C. I'm trying to say that experience with asm greatly helps to to see a difference between `foo`, `inline foo` and `virual foo`.
- virtualized 9y agotl;dr: If you use C++ like Java and don't care about performance at all, it will be half as efficient as hand-optimized C. Which is pretty remarkable actually. In favor of C++. Why didn't the author rewrite it in C++? It's not like C can do anything that C++ can't.