Example #1
0
/*
 * Function: RunMemoryTrial
 * ------------------------
 * Fills a pqueue to specified size and reports memory usage.  Then does
 * a bunch of enqueue-dequeue operations to jumble things up and reports
 * on the memory usage again. Reports results to cout.
 */
void RunMemoryTrial(int size)
{
    PQueue pq;
    cout << endl << "Running memory trial on " << size << "-element pqueue" << endl;
    for (int i = 0; i < size; i++)
		pq.enqueue(RandomInteger(1, size));
    cout << "After consecutive enqueues, " << size << "-element pqueue is using "
         << pq.bytesUsed()/1000 << " KB of memory" << endl;
    int num = size;
    for (int j = 0; j < NumRepetitions; j++) { /* do a bunch of enqueue/dequeue ops */
	    if (RandomChance(.5)) { 
	        pq.enqueue(RandomInteger(0, size));
	        num++;
        } else {
	        pq.dequeueMax();
	        num--;
	    }
    }
    cout << "After more enqueue/dequeue, " << num << "-element pqueue is using "
         << pq.bytesUsed()/1000 << " KB of memory" << endl;
}