While I was submitting solution for http://www.codeforces.com/contest/86/problem/D, I was getting TLE.
So, I searched for getting faster inputs in C++. Somehow, It was Accepted. But, I didn't understand weird behavior of inline functions.
I think using inline functions make program runs faster, but in my case it was making TLE.
My Submissions with minor changes in 'readLongLong' function:
- No Inline and Pass By Reference : Accepted
http://www.codeforces.com/contest/86/submission/18234613
- Inline and Pass By Reference : TLE at test 42
http://www.codeforces.com/contest/86/submission/18234625
- No Inline and value return : TLE at test 42
http://www.codeforces.com/contest/86/submission/18234694
- Inline and value return : TLE at test 51
http://www.codeforces.com/contest/86/submission/18234684
Inline with Pass By Reference : TLE
inline void readLongLong (ll &val) {
char ch;
val = 0;
while (true)
{
ch = getchar();
if (ch < '0' || ch > '9') break;
val = val*10 + (ch - '0');
}
}
No Inline with Pass By Reference : Accepted
void readLongLong (ll &val) {
char ch;
val = 0;
while (true)
{
ch = getchar();
if (ch < '0' || ch > '9') break;
val = val*10 + (ch - '0');
}
}
"Chapter 15: The inline disease
There appears to be a common misperception that gcc has a magic "make me faster" speedup option called "inline". While the use of inlines can be appropriate (for example as a means of replacing macros, see Chapter 12), it very often is not. Abundant use of the inline keyword leads to a much bigger kernel, which in turn slows the system as a whole down, due to a bigger icache footprint for the CPU and simply because there is less memory available for the pagecache. Just think about it; a pagecache miss causes a disk seek, which easily takes 5 milliseconds. There are a LOT of cpu cycles that can go into these 5 milliseconds.
A reasonable rule of thumb is to not put inline at functions that have more than 3 lines of code in them. An exception to this rule are the cases where a parameter is known to be a compiletime constant, and as a result of this constantness you know the compiler will be able to optimize most of your function away at compile time. For a good example of this later case, see the kmalloc() inline function.
Often people argue that adding inline to functions that are static and used only once is always a win since there is no space tradeoff. While this is technically correct, gcc is capable of inlining these automatically without help, and the maintenance issue of removing the inline when a second user appears outweighs the potential value of the hint that tells gcc to do something it would have done anyway."
(c) Linux Kernel Coding Style
Just to add — inline is only recommendation to compiler. It has freedom to choose whether to inline or not based on many factors, including optimization, specifics of the function and so on.