0

I have written one program to calculate the execution time of the function. I want to display the execution time of it. I am using chrono library and using the high_precision_clock function of it. But still I am getting runtime as 0.

void primsCall(){
    Prims prm(vec, adjMatrix);
    auto start = high_resolution_clock::now();
    prm.formMST(vec, adjMatrix);
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<nanoseconds>(stop-start);
    cout<<"Prim's Algorithm MST: (total cost: "<<prm.mstCost<<"; runtime: "<<duration.count()<< "ms)"<<endl;
    for(string entry : prm.path){
        cout<<entry<<endl;
    }
    return;
}

Also I have used different function of chrono:

void kruskalSCall(){
    cout<<endl<<endl;
    Kruskal ksk(vec, adjMatrix);
    clock_t start, end;
    start = clock();
    ksk.formMST(vec);
    end = clock();
    double runtime = double(end - start);
    cout<<"Kruskal's Algorithm MST: (total cost: "<<ksk.kMstCost<<"; runtime: "<<runtime<< "ms)"<<endl;
    for(string entry : ksk.kPath){
        cout<<entry<<endl;
    }
    return;
}

But in both the cases my output is 0.

Prim's Algorithm MST: (total cost: 15; runtime: 0ms) (1,3) (3,6) (6,4) (3,2) (2,5)

Kruskal's Algorithm MST: (total cost: 15; runtime: 0ms) (1,3) (4,6) (2,5) (3,6) (2,3)

Please suggest me to calculate the time of execution.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • 2
    Run it 1,000,000 times and then divide total time by 1,000,000 for example. – CherryDT Nov 01 '21 at 11:16
  • Are you on Windows by chance? IIRC the `high_resolution_clock` on windows only has a resolution of 15ms. – 0x5453 Nov 02 '21 at 13:16
  • @0x5453 according to [doc.microsoft.com](https://learn.microsoft.com/en-us/cpp/standard-library/chrono?view=msvc-160) it's nanosecond... so citation needed. – Mgetz Nov 02 '21 at 13:28
  • @Mgetz Searching for "windows 15.6ms" gives [quite](https://randomascii.wordpress.com/2013/07/08/windows-timer-resolution-megawatts-wasted/) [a](https://stackoverflow.com/q/40594587/3282436) [few](https://www.intel.com/content/www/us/en/develop/documentation/vtune-help/top/reference/energy-analysis-metrics-reference/timer-resolution.html) [results](https://stackoverflow.com/q/36696780/3282436), but I ran a few tests locally with `std::chrono::high_resolution_clock::now()` and it seems to work fine - I guess the latest Windows implementation of `chrono` has ways to work around the constraints? – 0x5453 Nov 02 '21 at 16:03
  • 1
    @0x5453 timers are not clocks. There is a huuuuge difference between tick rate and what [`FileTimeToSystemTime`](https://learn.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-filetimetosystemtime) (which is what chrono uses under the hood on windows. The tick rate is irrelevant here because they aren't interested in wake ups but wall time. Windows since windows 8 has supported dynamic tick anyways. – Mgetz Nov 02 '21 at 16:25

0 Answers0