Wednesday, July 10, 2013

What is faster: multidimensional array or inline function?


Suppose given simple function which depends from four parameters f(i, j, c, k) = abs(i-c) + abs(c-k), where i, j, c, k are less than 100.
The question is: what is the faster way: use inline function which compute each result every time or to build multidimensional array res[i][j][c][k] once with all possible answers?

I have checked this case with i, j, c, k is less than 45 with using gcc 4.7.2.
Here are results: 
Time for array is: 5.410000 sec. 
Time for inline function is: 5.410000 sec. 

Conclusions. Time of executions is the same, because inline function is quite simple. Benefit of inline function is more understandable way of result calculation without using extra memory. So, if function is quite simple there is no reason to create multidimensonal array with cached results.

Below is the code which I used to check performance.

No comments:

Post a Comment