# How can I keep track on time for each sort algorithm?

Page 1 of 1

## 6 Replies - 190 Views - Last Post: 07 December 2019 - 09:04 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=418015&amp;s=93c0f9e783de781bfd8ab77238f3a991&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mike10lanurias

Reputation: 0
• Posts: 13
• Joined: 20-November 19

# How can I keep track on time for each sort algorithm?

Posted 07 December 2019 - 05:55 PM

I tried to figure out how can I keep track of time from the beginning of each sorting algorithm to the end by using Chrono. Can I use that or something else?

This is the code that I'm doing by far.

```#include <iostream>
#include <chrono>

using namespace std;
using namespace std::chrono;

#include "Heapsort.h"
#include "InsertionSort.h"
#include "ModQuickSort.h"
#include "QuickSort.h"
#include "SelectionSort.h"

int main()
{
const int SIZE = 42500;
int numbers[SIZE];
cout << "Sorting 42500 integers" << endl << endl;

for (int j = 1; j < 6; j++)
{

for (int i = 0; i < SIZE; i++)
{
numbers[i] = (rand() % 100 + 1);
}
cout << "Trial #" << j << endl << endl;

auto start = high_resolution_clock::now();
quickSortMod(numbers, 0);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(stop - start);
cout << "Modified quicksort elapsed time: "
<< duration.count() << " nanoseconds" << endl;
auto start = high_resolution_clock::now();
quickSort(numbers, 0, SIZE);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cout << "Quicksort elapsed time: "
<< duration.count() << " milliseconds" << endl;
auto start = high_resolution_clock::now();
heapSort(numbers, SIZE);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cout << "Heapsort elapsed time: "
<< duration.count() << " milliseconds" << endl;
auto start = high_resolution_clock::now();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cout << "Radix sort estimated time: "
<< duration.count() << " milliseconds" << endl;
auto start = high_resolution_clock::now();
insertionSort(numbers, SIZE);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cout << "Insertion Sort elapsed time: "
<< duration.count() << " milliseconds" << endl;
auto start = high_resolution_clock::now();
selectionSort(numbers, SIZE);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cout << "Time taken by function: "
<< duration.count() << " milliseconds" << endl;
}
return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: How can I keep track on time for each sort algorithm?

### #2 #define

• Duke of Err

Reputation: 1862
• Posts: 6,710
• Joined: 19-February 09

## Re: How can I keep track on time for each sort algorithm?

Posted 07 December 2019 - 06:22 PM

Hi, what is the problem you are having?

The high_resolution_clock might be quite fine, the steady_clock might be what you are after.

There are also time methods in the C library.

When you declare variables with auto you shouldn't need to use auto on the same variables.

### #3 mike10lanurias

Reputation: 0
• Posts: 13
• Joined: 20-November 19

## Re: How can I keep track on time for each sort algorithm?

Posted 07 December 2019 - 06:29 PM

I want these algorithms to start simultaneously and stops if they all have the elapsed time.

### #4 mike10lanurias

Reputation: 0
• Posts: 13
• Joined: 20-November 19

## Re: How can I keep track on time for each sort algorithm?

Posted 07 December 2019 - 06:35 PM

Also, start, stop, and definition would have a redefine for basic data types. Error 2371.

### #5 #define

• Duke of Err

Reputation: 1862
• Posts: 6,710
• Joined: 19-February 09

## Re: How can I keep track on time for each sort algorithm?

Posted 07 December 2019 - 07:21 PM

mike10lanurias, on 08 December 2019 - 01:29 AM, said:

I want these algorithms to start simultaneously and stops if they all have the elapsed time.

To have them start at the same time would mean using multi-threading - which is more complicated.

To stop after a time interval would require the sort functions checking whether a time interval has been reached.

You may wish to create a class to simplify.

```class stopwatch
{
public:
start();
stop();
duration();

set_elapsed(int seconds);

// etc
};

```

### #6 mike10lanurias

Reputation: 0
• Posts: 13
• Joined: 20-November 19

## Re: How can I keep track on time for each sort algorithm?

Posted 07 December 2019 - 07:48 PM

Thanks, I fixed the problem. But I'm still having trouble with the Radix sort header file.

### #7 Skydiver

• Code herder

Reputation: 7187
• Posts: 24,357
• Joined: 05-May 12

## Re: How can I keep track on time for each sort algorithm?

Posted 07 December 2019 - 09:04 PM

Do you mind sharing how you got all the sorting algorithms to start simultaneously, and how you go them to stop after hitting some timeout value? How will your solution work on a machine that has a single CPU that has no threading support?