I am sitting in front of a brandnew 27 inch Apple Macintosh running on OS X Yosemite, trying to compile a timed insertion sort from my Windows days.

The Xcode app is a sweatheart, but there are some Windows things in the code that cause a problem.

This line ...
#include <windows.h> // GetTickCount()
does not work of course.

Does anyone know what I can use instead of windows.h and function GetTickCount()?

Recommended Answers

All 3 Replies

Some 10-15 years ago I had a Mac. I used something called BootCamp to switch between Mac and Windows. Worked very good. There is also something like it called Parallels.
But if it is only GetTickCount, I'm sure there must be some sort of Timer lib on the Mac with a function with this functionality. Mac is Unix based maby that helps also.

Thanks ddanbe, I am fiddling with
#include <sys/time.h>

With some more searching and fiddling I came up with this solution ...

//
//  main.c
//  insertion_sort
//
// insertionSort101.c
// time the insertion sort of an array of random integers


#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>

#define NUM_ITEMS 10000

void insertionSort(int numbers[], int array_size);

int numbers[NUM_ITEMS];

// calculate time difference using seconds and microseconds
// return value in milliseconds
float timedifference_msec(struct timeval ts, struct timeval te)
{
    return (te.tv_sec - ts.tv_sec)*1000.0f + (te.tv_usec - ts.tv_usec)/1000.0f;
}

int main()
{
    int k;
    float elapsed;
    struct timeval te, ts;
    clock_t ticks1;

    // seed random number generator
    ticks1 = clock();
    printf("seed = %ld\n", ticks1);
    srand((int)ticks1);

    // fill array with random integers
    for (k = 0; k < NUM_ITEMS; k++)
        numbers[k] = rand();

    gettimeofday(&ts, 0);

    // perform insertion sort on array
    insertionSort(numbers, NUM_ITEMS);

    gettimeofday(&te, 0);
    elapsed = timedifference_msec(ts, te);
    printf("Insertion sort of %d random integers took %f ms\n",
           NUM_ITEMS, elapsed);

    printf("Check first 10 sorted numbers:\n");
    for (k = 0; k < 10; k++)
        printf("%d\n", numbers[k]);

    //getchar();  // console wait
    return 0;
}

void insertionSort(int numbers[], int array_size)
{
    int i, j, temp;

    for (i = 1; i < array_size; i++) {
        temp = numbers[i];
        j = i;
        while ((j > 0) && (numbers[j-1] > temp)) {
            numbers[j] = numbers[j-1];
            j = j - 1;
        }
        numbers[j] = temp;
    }
}

/* possible result ...
 seed = 2795
 Insertion sort of 10000 random integers took 62.276978 ms
 Check first 10 sorted numbers:
 41043
 63844
 259373
 436615
 497201
 503268
 595312
 682174
 995187
 1410561
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.