1

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++;

    }   
user977154
  • 1,045
  • 4
  • 19
  • 39

3 Answers3

3

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).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

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.

DejanLekic
  • 18,787
  • 4
  • 46
  • 77
0

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).

Community
  • 1
  • 1
OSH
  • 2,847
  • 3
  • 25
  • 46