2

I'm trying to calculate time of 3 different sort algorithms. When the program is working , my cpu usage in this application is %12 in task manager and i want to run it with max cpu power. How can i do it?

...

auto t1 = Clock::now();
insertionSort(A1, size);
auto t2 = Clock::now();

cout << "insertionSort time: ";
cout << std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count() / 1000000000.0f;
cout << " Seconds.\n";

...

Working correctly but not using all cpu.

caglarinhesap
  • 51
  • 1
  • 6
  • 1
    What is in this sort implementation that allows it to make use of multiple CPU cores? 12% is (essentially) 1/8, so I suspect you have a non-concurrent sort on an 8 core CPU. – Richard Oct 11 '19 at 06:54
  • @Richard Isn't the percentage usually per-core? So you could in fact use up to 800% CPU. – BoBTFish Oct 11 '19 at 06:55
  • 1
    @BoBTFish depends on how it is measured. If you are looking at Task Manager (or Process Explorer) then 100% is all CPUs each at 100%. – Richard Oct 11 '19 at 06:57
  • I have never done it my self but maybe you can increase the priority of your program with [`SetPriorityClass`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setpriorityclass?redirectedfrom=MSDN) – wes Oct 11 '19 at 07:01
  • @BoBTFish I have same result in small inputs too. – caglarinhesap Oct 11 '19 at 07:08
  • @Richard My cpu is i7 7700k , this have 4 cores right? – caglarinhesap Oct 11 '19 at 07:08
  • A i7 7700k is indeed 4 cores, but it is also two hardware threads per core (Intel calls this "Hyper-threading", so the OS and applications see it as 8 core. – Richard Oct 11 '19 at 07:11
  • @wes i have the same result. – caglarinhesap Oct 11 '19 at 07:21
  • @Richard yes , you are right. Thanks. – caglarinhesap Oct 11 '19 at 07:22

1 Answers1

4

There are probably two different causes of this.

Firstly, your program is probably not CPU-bound. That is, the CPU is not the bottle-neck so it has to idle while memory access et.c. is taking place.

Secondly, your program is not Multi-threaded. That is, it cannot take advantage of the multiple cores in your CPU. There are parallel sorting algorithms you can take a look at. Here is a good starting point: Which parallel sorting algorithm has the best average case performance?

Pibben
  • 1,876
  • 14
  • 28