0

How would one go about optimizing their code?

I'm really curious. If it's something I can start practicing now, I could get a good head start. I'm wondering not just about functions, but classes, loops, etc. Any examples would be a great help.

19th Mar 2017, 6:32 PM
Rain
Rain - avatar
3 odpowiedzi
+ 3
There are several books and resources for optimizing different aspects of c++ code. Understanding algorithm complexity will go a long way. For example, reducing complexity (like talking something from O(n^2) to O(log(n))) will optimize your code much more than most other "tricks". A lot of the other optimizations are going to be little things you learn along the way. Like for(int i=0; i<n; ++i) will run at worst as fast as i++, but may be optimized by the compiler to be faster. It executes the for loop exactly the same (try it!). Understanding trees, pointers, and patterns (singleton, factories, etc.) can go a long way.
20th Mar 2017, 2:35 AM
Null
+ 1
Example: Cout << "hello"; Cout << "hello"; Cout << "hello"; Cout << "hello"; To: for(int i = 1;i < 4;i++) { Cout << "hello"; }; Or: int a = 10; int b = 10; cout << a + b; To: int a,b = 10; cout << a + b; You can optimize code in a bunch of ways.
19th Mar 2017, 11:06 PM
WakeCode
WakeCode - avatar
0
@WakeCode But does one run faster than the other?
19th Mar 2017, 11:08 PM
Rain
Rain - avatar