Performance of C++ code

I try to make my problems shorter, but even sometimes basic coded lines show TIME EXCEEDED. How do i improve the time performance of my algoritm. Please help :frowning:

Its not that how short you write the code, more important is how good you write code. Basic TLE errors are due to taking inputs where we generally use cin and cout instead use scanf and printf.

Some of the points are well explained here and here

EDIT:
In addition to these points head on to ProjectEuler and try solving the questions after solving you will get best way to program your just answered questions, It will help you in developing efficient algos be it in any Language :slight_smile:

1 Like

licr said it all and explained the most common problem… but sometimes (more often than we all would like to) the TLE error arises from having developed a not very efficient algorithm… On that case you should focus more on the logic and mathematical formalisms of the problem instead of focusing on code issues :slight_smile:

It’s all a matter of practice and earning experience!!

Bruno

While optimization of C++ code helps, Codechef made me think more in terms of asymptotic complexity.

So if a problem has a O(n log n) solution, O(n^2) is surely going to be TLE. And no amount of performance boost is going to work. And you will keep wondering, why TLE?

So if you get TLE, first check if your algorithm is appropriate. Can you lower its complexity? If it seems optimal, then look for optimization, like using faster I/O. But sometimes, people end up doing optimization, which is in fact slowing down the solution. So you should measure them as well.

1 Like