6 Replies - 190 Views - Last Post: 07 December 2019 - 09:04 PM Rate Topic: -----

#1 mike10lanurias   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 "RadixSort.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();
		radixsort(numbers, SIZE);
		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   User is offline

  • Duke of Err
  • member icon

Reputation: 1862
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 mike10lanurias   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 mike10lanurias   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1862
  • View blog
  • 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

View Postmike10lanurias, 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
};


Was This Post Helpful? 0
  • +
  • -

#6 mike10lanurias   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7187
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1