3 ms·
Well, I took the bait. Time to go through an unsorted vs sorted array on an iMac17,1 with 4Ghz Core i7 (not sure which CPU exactly).: * 1MB: 6.52ms vs 2.16ms (
by AceJohnny2 8y ago
Well, I took the bait. Time to go through an unsorted vs sorted array on an iMac17,1 with 4Ghz Core i7 (not sure which CPU exactly).:
* 1MB: 6.52ms vs 2.16ms (3x speedup)
* 1GB: 5.73s vs 2.25s (2.5x speedup)
Tested with this C code, called as "bpredict-time [size] [sort]":
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/time.h>
#define SIZE (1024*1024)
int cmp (const void *val1,
const void *val2)
{
return *(uint8_t*)val1-*(uint8_t*)val2;
}
int
main (int argc,
char **argv)
{
uint8_t *array;
size_t size = SIZE;
struct timeval start, end;
if (argc > 1) {
size = atoi(argv[1]);
}
array = malloc(sizeof(*array) * size);
if (array == NULL) { return -1;}
for (size_t i=0; i<size; i++) {
array[i] = random();
}
if (argc > 2) {
qsort(array, size, sizeof(*array), cmp);
}
(void)gettimeofday(&start, NULL);
int cnt = 0;
for (size_t i=0; i<size; i++) {
if (array[i] < 128) { cnt++;}
}
(void)gettimeofday(&end, NULL);
printf("N ints < 128: %d\n", cnt);
printf("Time: %f\n", (float)(end.tv_sec-start.tv_sec)\
+(float)(end.tv_usec-start.tv_usec)/1000000);
free(array);
return 0;
}
- Sohcahtoa82 8y agoWhy do you use "(void)gettimeofday(&start, NULL);" instead of just "gettimeofday(&start, NULL);"?
- AceJohnny2 8y agoTo avoid the compiler complaining about the unused return value, by telling it I'm explicitly choosing to ignore the value. The complaint doesn't show up on the default warning levels. I think only with -W ? (fun fact: -Wall actually has fewer warnings enabled than -W, at least with Clang) Edit: actually, the warning is enabled by default, but only triggers for functions that have the attribute 'warn_unused_result' set, which is almost none of them, which is why this warning is not well-known. Indeed, in my example my (void) isn't necessary, and I couldn't get the warning to trigger even with -Wpedantic.