0

I looked up a bunch of other examples of how to time my code but none of the ones that utilized either chrono or time seemed to work (they return 0). However, what did work QueryPerformanceCounter. The only downside to using it is that it is only available for Windows as I read. My instructor uses a Mac so I can not hand this code to him. This is what my code looks like when I use the QueryPerformanceCounter.

#include <iostream>
#include "heapsort.h"
#include "quicksort.h"
#include "insertionsort.h"


using namespace std;


#include <windows.h>
//THE FOLLOWING CODE RETURNS RUNNING TIME IN MICROSECONDS. 
//https://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter
double PCFreq = 0.0;
__int64 CounterStart = 0;

void StartCounter()
{
    LARGE_INTEGER li;
    if (!QueryPerformanceFrequency(&li))
        cout << "QueryPerformanceFrequency failed!\n";

    PCFreq = double(li.QuadPart) / 1000000.0;

    QueryPerformanceCounter(&li);
    CounterStart = li.QuadPart;
}
double GetCounter()
{
    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    return double(li.QuadPart - CounterStart) / PCFreq;
}


int main(){
    static const size_t SIZE = 150;
    int arr[] = { 685, 119, 938, 836, 721, 801, 738, 334, 739, 89, 917, 277, 708, 905, 978, 84, 620, 948, 409, 891, 447, 957, 673, 627, 546, 137, 456, 594, 878, 972, 722, 934, 383, 628, 103, 604, 132, 2, 428, 893, 212, 629, 646, 382, 348, 49, 306, 707, 156, 373, 733, 419, 323, 825, 112, 930, 432, 862, 830, 69, 994, 600, 226, 570, 759, 988, 289, 75, 232, 167, 292, 644, 10, 679, 607, 522, 967, 341, 989, 130, 326, 816, 503, 794, 303, 108, 915, 148, 258, 73, 206, 701, 897, 350, 713, 940, 764, 471, 936, 93, 163, 824, 950, 796, 98, 823, 465, 37, 102, 342, 243, 696, 687, 935, 459, 50, 553, 225, 562, 181, 453, 665, 525, 175, 768, 251, 996, 954, 925, 531, 962, 585, 250, 829, 777, 928, 76, 704, 565, 20, 422, 51, 125, 197, 588, 267, 850, 494, 699, 173 };


    StartCounter();
    heapSort<int> heap(arr, SIZE);
    cout << GetCounter() << endl;
    StartCounter();
    quickSort<int> quick(arr, 0, SIZE-1);
    cout << GetCounter() << endl;
    StartCounter();
    insertionSort<int> insertion(arr);
    cout << GetCounter() << endl;
    return 0;
}
Community
  • 1
  • 1
MD Islam
  • 137
  • 8
  • Are you asking for _performance profiling code injections_? That's e.g. available with `gprof`. Which toolchain are you forced to use? – πάντα ῥεῖ Nov 29 '14 at 17:43
  • @πάνταῥεῖ I'm not sure what that means. Google doesn't return any definitions for it either. I need to get how long each sorting algorithm takes. It takes microseconds but I still need that to compare the three sorting algorithms. I'm on a Windows, by the way. I don't think there is a tool I'm forced to use but it just needs to work when the instructor runs the code. – MD Islam Nov 29 '14 at 17:47
  • On Windows, you may have problems to get correct microsecond resolutions for any operations. (Tenth of) milliseconds is the best bet you'll get. – πάντα ῥεῖ Nov 29 '14 at 17:50
  • @πάνταῥεῖ That's what seemed to me as I was researching ways to do this. That's why I asked this question even though there are so many of the same (none seemed sufficient, unless there's something I missed). – MD Islam Nov 29 '14 at 17:53
  • 1
    It just doesn't matter that your clock source has a low resolution. Repeat the code a million times, divide the measurement by a million. – Hans Passant Nov 29 '14 at 18:22
  • Had not thought of that!! – MD Islam Nov 30 '14 at 18:17

1 Answers1

0

If all platforms are running Intel, you could look at using the Time Stamp Counter (RDTSC) and write your own performance counter around this. But its not very portable and there are various tricky bits (drift between cores, variable clock frequency unless you have that disabled, etc). Generally I go for the low-tech approach of doing the task at hand a large number of times and then getting an average time via a low-tech timer (even time a.out at the shell).

sfjac
  • 7,119
  • 5
  • 45
  • 69
  • Thanks. I used the low-tech timer method so it can be run on someone else's PC. Hans Passant also suggested this method. Kudos to the both of you. – MD Islam Dec 02 '14 at 03:58