koper
koper

Reputation: 7

Compare how long two functions took

I have two functions and I want to print how long each of them took to finish.

What I have so far:

int main()
{
   clock_t tic1 = clock();
   shell_sort(array, 100000);
   clock_t toc1 = clock();
   printf("shellsort took: %f seconds\n", (double)(toc1 - tic1) / CLOCKS_PER_SEC);

   clock_t tic2 = clock();
   bubble_sort(array, 100000);
   clock_t toc2 = clock();
   printf("bubblesort took: %f seconds\n", (double)(toc2 - tic2) / CLOCKS_PER_SEC);

   return 0;
}

It should work fine, but after printing duration of first function it stops. It feels like the program is still running. There is no "Press any key to continue". How can I fix this?

Upvotes: 0

Views: 36

Answers (1)

adrian008
adrian008

Reputation: 405

I assume your sort functions are fine.

You are trying to check the time it takes bubble sort to sort 100,000 values. I guess you know its quite slow. Have patience and wait.

enter image description here

As you can see for a hundred thousand entries you should wait almost a minute.

Upvotes: 2

Related Questions