So I know similar questions have been asked before, but I didn't quite get what I was looking for.
I want to have a function, separated into .h and .cpp file, which can perform some calculations on a 2D-array and modify the array during the calculation. Currently my code looks something like this:
main.cpp:
int gridsize = 100; // usually in range 50-200
int **grid = ...; // define dynamic array
myFunction(grid, gridsize);
myFunction.h:
void myFunction(int **grid, int gridsize);
myFunction.cpp:
void myFunction(int **grid, int gridsize)
{
for(int step = 0; step <= MAX_STEPS; step++)
{
int i = ...;
int j = ...;
grid[i][j] = someCalculation();
}
}
...and currently this works as I want it to.
Now, the question is, is this the only/best way of doing it? myFunction should be applicable to different array sizes unknown at compile time and I am looking for good performance, since myFunction will be called ~10^6 times during execution and each call will perform ~10^5 array accesses, which currently corresponds to runtimes in the range of hours.
I would know how to do it with std::vector
, but that appears to be roughly 1.5 times slower than my current code. I have also seen some approaches with std::array
and template
functions, where I run into trouble with the unknown array size (...and with known array size, again, it appears to take almost twice the time to do the calculations).