0

I was benchmarking two simple sorting algoritm: selection and insertion sort. I use clock() function to measure how much time each function use. However, there is an interesting phenomenon: when the size of random array is 1000 the exuction time sometimes will be 0.0000 sec. When the size increased to 10000, everything go back to normal.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>

static inline void swap(int *restrict const a, int *restrict const b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

static void selection(int *restrict const array, size_t size) {
    for (size_t i = 0; i < size; i++) {
        int min_index = i;
        for (size_t j = i+1; j < size; j++) {
            if (array[j] < array[min_index]) {
                min_index = j;
            }
        }
        swap(&array[min_index], &array[i]);
    }
}

static void insertion(int *restrict const array, size_t size) {
    for (size_t i = 1; i < size; i++) {
        for (size_t j = i; j > 0; j--) {
            if (array[j] < array[j-1]) {
                swap(&array[j], &array[j-1]);
            } else {
                break;
            }
        }
    }
}

int main(int argc, char const *argv[])
{
    int rand_array1[10000];
    int rand_array2[10000];
    srand(time(NULL));
    for (size_t i = 0; i < 10000; i++) {
        rand_array1[i] = rand();
    }
    memcpy(rand_array2, rand_array1, sizeof(rand_array1));
    clock_t start = clock();
    insertion(rand_array1, 10000);
    clock_t stop = clock();
    printf("Insertion use %lf sec.\n", (double)(stop - start) / CLOCKS_PER_SEC);
    start = clock();
    selection(rand_array2, 10000);
    stop = clock();
    printf("Selection use %lf sec.\n", (double)(stop - start) / CLOCKS_PER_SEC);
        return 0;
}

Output(array size 1000):

[william@Notebook Sort]$ ./a.out 
Insertion use 0.002276 sec.
Selection use 0.006772 sec.

[william@Notebook Sort]$ ./a.out 
Insertion use 0.001282 sec.
Selection use 0.000000 sec.

Output(array size 10000):

[william@Notebook Sort]$ ./a.out 
Insertion use 0.153155 sec.
Selection use 0.102534 sec.

[william@Notebook Sort]$ ./a.out 
Insertion use 0.161796 sec.
Selection use 0.141816 sec.
user762750
  • 159
  • 7
  • 1
    That's normal. Google "c high resolution timer" to find alternatives. – Hans Passant May 27 '19 at 14:17
  • On POSIX systems, the return value of `clock()` is expressed in microseconds, but that does not imply that times can actually be measured to microsecond precision via that interface. – John Bollinger May 27 '19 at 14:20
  • @HansPassant clock() is already a high resolution clock function in c, sorting such size of random array is never going to take time less than 0.000001.. – user762750 May 27 '19 at 14:21
  • It is not, as your test demonstrates fairly well. Code execution on modern machines is also heavily non-deterministic, repeating a test at least 10 times is necessary to find a trustworthy median value. And you must srand() with the same value to get the same measurement. – Hans Passant May 27 '19 at 14:26
  • The man-page states `The clock() function returns an approximation of processor time used by the program`. I think that the function might be accessing a fine-grained counter (able to count microseconds) that gets updated at course granularity. `clock()` might return the same value if called before it gets updated. – TSG May 27 '19 at 14:27
  • Check this [thread](https://stackoverflow.com/questions/6498972/faster-equivalent-of-gettimeofday/6499061#6499061) for details of finer counters/clocks. The best is obviously to use the `__rdtscp()` from `x86intrin.h` at the cost of portability. – TSG May 27 '19 at 14:40

0 Answers0