1

I have an assignment to plot the running time of a quicksort algorithm(implemented myself) with both versions i.e randomised and deterministic(picking last element as pivot).

Now I have very large lists of integers and also relatively short ones [range : (500 - 10^6) ]. Now as the worst case complexity of Quicksort is O(n^2) I assume the code could take more than 30 mins to execute and I want to measure the CPU time taken by the process as is expected from std::clock however I am on a Windows system and std::clock returns the same time as the Wall clock. This is an issue as I want precise time that the process takes to execute and isn't affected by other processes on my computer.

So far I have searched on some questions on this website and codereview ; nothing seems to be doing what I want.

I also have Ubuntu installed on WSL2 on my system, so if there is way to do the same there, that'd help me too.

So my question is : How do I find exact time taken by the process on the CPU for my sorting algorithms?

(PS: This website seems to suggest that the std::clock may wrap after 36mins(approx.) so if there is a way to note time in case the process takes more than that time would be helpful too.)

Hrishabh Nayal
  • 135
  • 1
  • 9
  • @Dai I think that does not give cpu time, https://en.cppreference.com/w/cpp/chrono/c/clock , in this website they use `std::chrono::high_resolution_clock::now()` to show the difference between wall time and cpu time in the example. – Hrishabh Nayal Sep 04 '22 at 17:03
  • 1
    The windows documentation page linked by the post suggests using [the GetProcessTimes function for Windows.](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getprocesstimes). What's wrong with that? – Avi Berger Sep 04 '22 at 17:33
  • @AviBerger I had not stumbled upon that function during my research. [QueryProcessCycleTime](https://learn.microsoft.com/en-us/windows/win32/api/realtimeapiset/nf-realtimeapiset-queryprocesscycletime)sounds like what I can use. Maybe I can look for the same. Thanks! – Hrishabh Nayal Sep 04 '22 at 17:39
  • I was just looking at it a bit more & it might not have adequate precision for your use. [See here](https://stackoverflow.com/q/43065154/631266) Edit: Oh QueryProcessCycleTime, didn't see that one. It might very well be better. – Avi Berger Sep 04 '22 at 17:44
  • Do not use `std::chrono::system_clock` to time code. Use `std::chrono::steady_clock` or `std::chrono::high_resolution_clock` instead. Simply grab the clock time at the beginning of the code, and again at the end of the code, and subtract to get the elapsed time. – Remy Lebeau Sep 05 '22 at 04:02

0 Answers0