0

I have code that generates random numbers from 1-100 and sorts them using the merge sort which I already have in a separate function. Everything works but when I implement clock(); to try and get the running time, I always get zero. I have even tried with larger numbers like 10000 but still, the time passed always gives me zero. here is my code

int main() {
clock_t startTime;
clock_t endTime;
clock_t timePassed;

int array[100];
srand(time(NULL));
int n = sizeof(array) / sizeof(array[0]);
startTime = clock();
    for (int j = 0; j < 100; j++)
    {
        array[j] = rand() % 100+1;
        std::cout << array[j] << " ";
    }
    std::cout << "\n";
    MergeSort(array, n);

    std::cout << "After Merge Sort :" << std::endl;
    PrintArray(array, n);

    endTime = clock();
    timePassed = ((endTime - startTime) / CLOCKS_PER_SEC);
    std::cout << "\n" << timePassed;
}
return 0;
}
m.Mig
  • 21
  • 2
  • 7
  • Is it giving you 0 or 0.00? could it be that it's rounding? Try `cout.precision(9999);` – dustytrash Sep 09 '18 at 04:47
  • try `float(timePassed)` – macroland Sep 09 '18 at 04:51
  • 6
    the `std::chrono` faculties of the standard library will give you better and more accurate results. – Casey Sep 09 '18 at 05:12
  • 2
    A bit of terminology: you don't *implement* `clock`, you just are using it. `clock` is *implemented* inside your C++ or C standard library (on Linux, using [clock_gettime(2)](http://man7.org/linux/man-pages/man2/clock_gettime.2.html) ...) – Basile Starynkevitch Sep 09 '18 at 05:36
  • May be useful https://stackoverflow.com/questions/44433440/how-to-check-time-performances-in-a-c-program-on-zedboard/44433887#44433887 – Galik Sep 09 '18 at 08:33
  • `clock_t` is an integer type, so `timePassed` will always be set to zero if your program takes less than 1 second to run. If you want finer granularity than one second, you should use floating point math rather than integer math when dividing by `CLOCKS_PER_SEC` (and store the result in a `float` variable) – Jeremy Friesner Sep 09 '18 at 13:43
  • thank you I changed timepassed to float type and I am receiving a very low number when I run the program with an array size of 100. I get 0.013 for the time passed. This seems more reasonable. – m.Mig Sep 09 '18 at 19:44

2 Answers2

1

use

double timePassed = (endTime - startTime) / static_cast<double>(CLOCKS_PER_SEC);

Plan B for higher accuracy:

#include <iostream>
#include <chrono>

// ...

auto start_time{ std::chrono::high_resolution_clock::now() };

// ... code you want to time

auto end_time{ std::chrono::high_resolution_clock::now() };

std::cout << std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time).count() << ":";
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count() << ":";

// ...
Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • changing my timepassed type to double and float seems to have given me a number that is not zero. Thank you – m.Mig Sep 09 '18 at 19:46
0

If you are developing on a Unix system and want to measure the execution time from an application, you can also use the 'time' command like:

time myapplication

see time (Unix) - Wikipedia

GizMoCuz
  • 3
  • 3