Is a 1000x speed gain possible here?
Hi,
I am writing an article and want to be sure of some numbers before I
send the article out. The difference in the run time numbers between
the for loop and the template method seem to be a little unrealistic
to me. I did expect a gain in speed but not by such a big amount.
I checked and double checked the code, to make sure I did not make any
mistakes, but everything seems to be right. I even replaced the timer
code with 2 other classes and both gave me the same results.
Is there something more coming into play here, like loop prediction on
the processor or something. In that case I'd expect it to work better
on the for loop.
I've reported the timing for each method, below each method's code.
[Begin code]
//###### Sample code 1
// Begin timing code
int SomeData[200], nPos, nFind ;
// Initialize SomeData with random values
// Start timer 1
for (int i=0; i<1000000; ++i)
{
nFind = -1 ;
for (int j=0; j<200; ++j)
{
if (nFind == SomeData[j])
{
nFind = i ;
break ;
}
}
}
// End timing code
// Best case here 0.00502243 seconds
// Worst case 0.297686 seconds
//###### Sample code 2
#define lenof(x) (sizeof(x)/sizeof(*(x)))
// Class declarations
template<int _I, typename _T>
class Find
{
public:
static inline int f(_T *Haystack, _T &Needle)
{ // Some code
if (Haystack[_I - 1] == Needle)
{
return _I - 1 ; // Our break statement
}
return Find<_I - 1, _T>::f(Haystack, Needle) ;
}
} ;
// Specialization stops the loop
template<typename _T>
class Find<0, _T>
{
public:
static inline int f(_T *Haystack, _T &Needle)
{
return -1 ;
}
} ;
// Begin timing code
for (int i=0; i<1000000; ++i)
{
nFind = Find<200, int>::f(SomeData, nFind) ;
}
// End timing code
// Best case here 1.11746e-006 seconds
// Worst case 1.53651e-005 seconds
[End code]
I'm using the QueryPerformanceCounter method to get time.
Please let me know if there is any mistake I've made with timing each
method.
Thanks,
- Sid
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]