Originally Posted by
wysota
creating a stack frame is more expensive than reading a variable or comparing it to some other variable.
that's certainly true; as I said I do not think descending/ascending will matter, but the pre/post-increment does.
If you want to make such statements, at least paste a profiler log or a compiler output to prove it
as you insist
here's a quick test, no optimisations used, post-increment standard for-loop vs pre-increment with variable for vector end:
void doSomething( unsigned )
{
}
void test( const size_t ac_nTestSize )
{
std::vector< unsigned > x( ac_nTestSize );
std::vector< unsigned > y( ac_nTestSize );
time::QueryPerformanceTimer tim;
tim.mp_Start();
std::vector< unsigned >::const_iterator cit;
for( cit = x.begin() ; cit < x.end() ; cit++ )
doSomething( *cit );
s_dbgpf( "post-inc took: %.3f", tim.mf_dElapsedMilliSeconds() );
tim.mp_Start();
std::vector< unsigned >::const_iterator cit2;
const std::vector< unsigned >::const_iterator citEnd( y.end() );
for( cit2 = y.begin() ; cit2 < citEnd ; ++cit2 )
doSomething( *cit2 );
s_dbgpf( "pre-inc took: %.3f", tim.mf_dElapsedMilliSeconds() );
}
void doSomething( unsigned )
{
}
void test( const size_t ac_nTestSize )
{
std::vector< unsigned > x( ac_nTestSize );
std::vector< unsigned > y( ac_nTestSize );
time::QueryPerformanceTimer tim;
tim.mp_Start();
std::vector< unsigned >::const_iterator cit;
for( cit = x.begin() ; cit < x.end() ; cit++ )
doSomething( *cit );
s_dbgpf( "post-inc took: %.3f", tim.mf_dElapsedMilliSeconds() );
tim.mp_Start();
std::vector< unsigned >::const_iterator cit2;
const std::vector< unsigned >::const_iterator citEnd( y.end() );
for( cit2 = y.begin() ; cit2 < citEnd ; ++cit2 )
doSomething( *cit2 );
s_dbgpf( "pre-inc took: %.3f", tim.mf_dElapsedMilliSeconds() );
}
To copy to clipboard, switch view to plain text mode
output for different sizes:
10:
post-inc took: 0.040
pre-inc took: 0.009
100:
post-inc took: 0.204
pre-inc took: 0.013
1000:
post-inc took: 2.823
pre-inc took: 0.113
10000:
post-inc took: 20.146
pre-inc took: 0.712
I have to say I really had no idea, until just now, that it makes such a difference..
Bookmarks