C++ - Performance of static arrays, with variable size at launch -
i wrote cellular automaton program stores data in matrix (an array of arrays). 300*200 matrix can achieve 60 or more iterations per second using static memory allocation (e.g. std::array).
i produce matrices of different sizes without recompiling program every time, i.e. user enters size , simulation matrix size begins. however, if use dynamic memory allocation, (e.g. std::vector), simulation drops ~2 iterations per second. how can solve problem? 1 option i've resorted pre-allocate static array larger anticipate user select (e.g. 2000*2000), seems wasteful , still limits user choice degree.
i'm wondering if can either
a) allocate memory once , somehow "freeze" ordinary static array performance?
b) or perform more efficient operations on std::vector? reference, performing matrix[x][y] == 1 , matrix[x][y] = 1 operations on matrix.
according this question/answer, there no difference in performance between std::vector or using pointers.
edit:
i've rewritten matrix, per umnyobe' suggestion, single array, accessed via matrix[y*size_x + x]. using dynamic memory allocation (sized once @ launch), double performance 5 iterations per second.
as per paulmckenzie's comment, compiled release build , got performance looking (60 or more iterations per second). however, foundation more, still want quantify benefit of 1 method on other more thoroughly, used std::chrono::high_resolution_clock time each iteration, , found performance difference between dynamic , static arrays (after using single array matrix representation) within margin of error (450~600 microseconds per iteration).
the performance during debugging slight concern however, think i'll keep both, , switch static array when debugging.
for reference, performing
matrix[x][y] - red flag! using
vector<vector<int>>for matrix representation? mistake, rows of matrix far apart in memory. should use single vector of sizerows x cols, usematrix[y * row + x] - furthermore, should follow approach index first rows , columns, ie
matrix[y][x]rathermatrix[x][y]. algorithm should process same way. due factmatrix[y][x](x, y) , (x + 1, y) 1 memory block each other while other mechanism elements(x,y),(x + 1, y),(x, y + 1)farther away.
even if there performance decrease std::array std::vector (as array can have elements on stack, faster), decent algorithm perform on same magnitude using both collections.
Comments
Post a Comment