Making a Program Fast

c
c-plus-plus
define
java

#1

Using #define for not only constants but for loops and for some statements, reduces time taken by the compiler to execute the code?


#2

No. #define is used to declare a macro and will not speed up a normal code you write.It is only provided as a convenience to make programming simpler. However replacing functions by macros will help speed up as function calls consume extra time. eg. #define square(x) x*x will be noticeably faster than a normal int square(int x) { return x * x } if number of function calls are large.

You might want to try fast Input/Output techniques as they can also help speed up code.


#3

Hello,

If you are planning on using such “hacks” to pass the TL, think again :slight_smile:

Usually, problem setters only use scanf/printf and/or cin/cout as their I/O routines, which means that usually, the right algorithm is the key to overcome TL. As it should be.

While not directly related to your question, this is useful to say that, if your code is not passing the TL without any defines, then, chances are, that adding them won’t make much difference, unless it exploits some special properties of the input format or from the problem in question, but, as a rule of thumb, this is not the way to go when it comes to optimize the code, and, here in Codechef, optimizing the code almost always means optimizing the algorithm :slight_smile:

Best,

Bruno