I'm going to assume that you've come to this conclusion because those variables are only referenced in the above methods. |
I came to this conclusion because I have my program spitting out time elapsed values, and when I comment out the function calls to these functions, the time goes down, whereas nothing else that I tried to temporarily remove affected the time as much.
There wouldn't be enough overhead to cause any sort of noticeable difference in speed. Just to double-check, I suggest making copies of your current files and attempting to revert back to the code you had before this, and comparing the difference. You might end up with the same results that you're getting now. |
I still have the original code, which is how I came up with the '3x-6x slower' numbers. So, unfortunately it does not seem to be just a fluke of the moment.
Additionally, can you give us some realistic examples of how far those for loops might be going? What's the "speed" that you were going at before, and what is it at now? |
The for loops are running for 120 and 430 iterations respectively. The speed of a single run through the function is not enough to record (at least not how I'm doing it with clock()), so I'm recording the speed of a single "trial" in the program, which is 60000 iterations of everything including those functions. These trials last .06 seconds in the old code, and .4 seconds in the new code when history is a vector, and .18 seconds in the new code when history is an array.
This might have something with cache coherency. When you allocate a 2D array it will be in a single piece of memory. When you allocate a vector of vectors, each subvector will call a new for it's piece of data so these vectors may end up in very different addresses. Try replacing the inner std::vector with an std::array or a structure containing 4 doubles. In this case the new operator will be called only once for the outer vector and the data will again be in a single contiguous piece of memory. If this indeed helps - then it's the cache-related problem. If not - follow NGen's recommendations (actually, follow them anyway :)) |
This is very interesting, I've never thought of making a vector of arrays. However my problem right now is not the speed difference between vectors and array, but between the old single-file code, and the new code with multiple files and classes. I also don't know how to create a vector of arrays like that. vector< double [4] > didn't work, vector< std::array<double, 4> > spits out that array isn't part of std, and vector< array<double, 4> > says array is not defined.
In the meantime I changed the interval functions to only sample every 10th entry in the history, without loss of performance.
Thanks for the responses,
Marc Dillon