Writing a program that demonstrate different sorting algorithm in C++ on Mac. I found two quicksort implementation, qsort and qsort_b.
The first one is of course the old-fashioned, seen-everywhere qsort. But there's qsort_b, which takes an block rather than a function. Here's my code:
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstdio>
#include <ctime>
#define DATA 1000000
using namespace std;
int compare(const void* a, const void* b)
{
return *(int*)a - *(int*)b;
}
int main(int argc, char *argv[])
{
int* array = new int[DATA];
srand(time(0));
for ( int i = 0 ; i < DATA ; ++ i )
{
array[i] = rand() % 2147483647;
}
clock_t begin = clock();
qsort(array, DATA, sizeof(array[0]), compare);
//qsort_b(array, DATA, sizeof(array[0]), ^(const void* a, const void* b) { return *(int*)a - *(int*)b; });
clock_t end = clock();
cout << "Time it takes to sort " << DATA << " integers with quicksort: " << end - begin;
}
Here I see big speed difference, what's causing all that difference. To my understanding, blocks is for parallel processing, which in this case won't be faster than functions. There's nothing to parallel process, is there?
EDIT: The heapsort_b(), mergesort_b(), and qsort_b() routines are like the corresponding routines without the _b suffix, expect that the compar callback is a block pointer instead of a function pointer. (FROM BSD MAN PAGE)
EDIT: The speed difference. With DATA being 1000000, qsort finished it in 146832 ns, with qsort_b, in 127391 ns. It's a relatively big difference considering it's about 10% faster.
EDIT: I've edited the code to make it possible to have even bigger array of integers. My personal biggest test result are 100000000 integers, 28136278 (28s) vs. 23870078 (24s). It's a considerably big difference to me.