headmyshoulder / odeint-v2

odeint - solving ordinary differential equations in c++ v2
http://headmyshoulder.github.com/odeint-v2/
Other
340 stars 101 forks source link

replace size_t i=0 by size_t i(0) #162

Closed GregorDeCillia closed 9 years ago

GregorDeCillia commented 9 years ago

While testing, i found that replacing

for( size_t i=0 ; i < dim ; ++i )

by

for( size_t i(0) ; i < dim ; ++i )

in array_algebra.hpp gives a better performance for the state types boost::array and std::array.

I also replaced these loops on other parts of the code just in case there are similar performance impacts.

ddemidov commented 9 years ago

Interesting. The following two code snippets result in identical assembler code (obtained with g++ -S). I wonder where the speedup comes from.

1.

#include <stddef.h>
int main() {
    int j = 0;
    for (size_t i(0); i < 10; ++i) ++j;
}

2.

#include <stddef.h>
int main() {
    int j = 0;
    for (size_t i = 0; i < 10; ++i) ++j;
}
GregorDeCillia commented 9 years ago

I was also kind of suprised that these changes made a difference. My tests used g++ with the Ofast flag as well as the boost_unit_test framework.

mariomulansky commented 9 years ago

Indeed quite surprising. You would think that this wouldnt make any difference to the compiler compiler...

May I ask how much of a performance improvement this gives? I would like to also check on the intel compiler before doing this merge.

GregorDeCillia commented 9 years ago

srange. i just created a simpler test case and created two assembly files one with and one without my commit and there does not seem to be any difference.

i guess i should have done more testing before creating a pull request. thanks for pointing that out @ddemidov

@mariomulansky please close this pull request. there is probably a problem with my test case