Problem 1676D : X-Sum
I solved the problem using brute force as the constrains were low. In my first try I used another function to do some iterations in a 2D matrix.
Looking at the editorial, they used the same logic but without using another function. So, I copy pasted my entire 'helper' function in my 'main-solver' function and the time difference was pretty huge. Should we avoid using functions in CP?
Using extra function : 168328426 Time taken : 1185ms
Without using another function : 168328365 Time taken : 46ms
Is this something we should take care of in contests?
Nope, calling a function takes time only when you passes arguments without the "reference", as passing the arguments without the reference, function needs to copy the arguments in some temporary memory to use it which takes extra time. i.e. copying an array or vector takes $$$O(size)$$$.
So if you pass with a reference the function willn't take any extra time. To do this you just need to add
&
before variable name.Like you may write your function as
int calculate_for(vector<vector<int>> &board, int i, int j, int rows, int cols)
instead ofint calculate_for(vector<vector<int>> board, int i, int j, int rows, int cols)
.Thank you. I tried submitting again and it passed with 30ms.
yes