In today's contest I got TLE
in one of the problems. I was using CPP (C++17 (GCC 7-32)
). As soon as I replaced method call (validSubstring
) to macro definition, solution got Accepted
. I went ahead and tried using the method version of my solution along with inline
keyword, which supposedly brings the performance of code similar to macro definition by inlining the method. However, this also resulted in TLE.
As far as my understanding goes, the reason behind failure of method version (without inline
keyword) of the submission is overhead of creating extra stack frames in thread stack, parameter-passing, type-check etc. Macro definition version probably passed as it does not do any of the above activities. But I am still not sure why the inline version is failing, is it because inline keyword is ignored by the pre processor use on CF, or the slight overhead difference between inlining and macro definition is causing this TLE.
Ref:
Auto comment: topic has been updated by shiv-aay (previous revision, new revision, compare).
In the function you are passing the string s by value meaning it get's copied for every function call. Try passing it by reference.
if you reference s you won't get TLE
bool validSubstring(string& s, int i) { return s.substr(i, 4) == "1100"; }