问题
I need to calculated the runtime of a hash insertion. i have been using clock to do the time but i keep ending up with zero. Is there any certain way that would be the most efficient?
This is my code for it so far:
cout << "Testing chaining probing...\n";
HashTable_chaining ChainingHT( ITEM_NOT_FOUND, 101 );
int i = 0;
while(i != DataArray.size())
{
clock_t tStart = clock();
ChainingHT.insert(DataArray[i]);
cout<<"Time taken:"<<(double)(clock() - tStart)/100000<<endl;
if(i != NULL)
{
collision_count++;
}
i++;
}
回答1:
a single hash insert is too quick to be measured. Put
clock_t tstart = clock();
at the start of your program, doing a million of insertions, and
clock_t tend = clock();
at the end. Then compute in floating point:
cout << "cpu time="
<< ((double)tend - (double)tstart) / CLOCKS_PER_SEC << endl;
Typical current computers do several billions elementary machine instructions per second (but with a clock resolution in milliseconds at best).
回答2:
My first guess it that insert is extremely fast so you get zeroes... I would never do what you try in this code. Instead I would make, say 10000 insertions, and then calculate how long it takes, and divide that number with 10000 to get average time it takes for insert.
回答3:
running a 10000 / 100000 / 1000000 insertion loop is ok (you need to play with the number of insertion until you get a value that doesn't take for ever to run).
If you are working in windows, consider using performance counters to getter a much better resolution (see code inside).
来源:https://stackoverflow.com/questions/8366657/calculating-the-runtime-of-a-hash-insertion