Created
September 29, 2016 06:44
-
-
Save izabera/d069eb12fa5d640564378a2f6163dbe8 to your computer and use it in GitHub Desktop.
sort -M comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <time.h> | |
int compmon1(const char *x, const char *y) { | |
struct tm thyme; | |
int dx; | |
char *xx,*yy; | |
xx = strptime(x,"%b",&thyme); | |
dx = thyme.tm_mon; | |
yy = strptime(y,"%b",&thyme); | |
if (!xx) return !yy ? 0 : -1; | |
else if (!yy) return 1; | |
else return dx==thyme.tm_mon ? 0 : dx-thyme.tm_mon; | |
} | |
int compmon2(const char *x, const char *y) { | |
// slightly edited version that i ended up using in my own code | |
int dx = 0, dy = 0; | |
char *months = "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec"; | |
for (int i = 1; i <= 12; i++) { | |
if (strncasecmp(months, x, 3) == 0) dx = i; | |
if (strncasecmp(months, y, 3) == 0) dy = i; | |
months += 3; | |
} | |
return dx && dy ? dx - dy : !dx && !dy ? 0 : dx ? 1 : -1; | |
} | |
int main() { | |
struct timespec t1, t2, t3; | |
int sum1 = 0, sum2 = 0; | |
char *months[] = { | |
"Jan", "feb", "Mar", "apr", "May", "jun", | |
"Jul", "aug", "Sep", "oct", "Nov", "dec" | |
}; | |
#define iterations 1000000 | |
clock_gettime(CLOCK_MONOTONIC, &t1); | |
srand(1); | |
for (size_t i = 0; i < iterations; i++) { | |
char *mon1 = months[rand()%12]; | |
char *mon2 = months[rand()%12]; | |
sum1 += compmon1(mon1, mon2); | |
} | |
clock_gettime(CLOCK_MONOTONIC, &t2); | |
srand(1); | |
for (size_t i = 0; i < iterations; i++) { | |
char *mon1 = months[rand()%12]; | |
char *mon2 = months[rand()%12]; | |
sum2 += compmon2(mon1, mon2); | |
} | |
clock_gettime(CLOCK_MONOTONIC, &t3); | |
printf("sum1: %d sum2: %d ", sum1, sum2); | |
printf("t2 - t1 = %f ", | |
((t2.tv_sec * 1000000000 + t2.tv_nsec) - | |
(t1.tv_sec * 1000000000 + t1.tv_nsec)) / 1000000000.0); | |
printf("t3 - t2 = %f\n", | |
((t3.tv_sec * 1000000000 + t3.tv_nsec) - | |
(t2.tv_sec * 1000000000 + t2.tv_nsec)) / 1000000000.0); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
izabera@notebook ~ $ gcc sortM.c -Wall -Wextra -O2 -o sortM | |
izabera@notebook ~ $ for i in {1..10}; do ./sortM; done | |
sum1: -2572 sum2: -2572 t2 - t1 = 4.798563 t3 - t2 = 1.377454 | |
sum1: -2572 sum2: -2572 t2 - t1 = 4.765520 t3 - t2 = 1.347284 | |
sum1: -2572 sum2: -2572 t2 - t1 = 4.856738 t3 - t2 = 1.405237 | |
sum1: -2572 sum2: -2572 t2 - t1 = 4.881066 t3 - t2 = 1.391032 | |
sum1: -2572 sum2: -2572 t2 - t1 = 4.884409 t3 - t2 = 1.356808 | |
sum1: -2572 sum2: -2572 t2 - t1 = 4.881681 t3 - t2 = 1.378078 | |
sum1: -2572 sum2: -2572 t2 - t1 = 4.932471 t3 - t2 = 1.361985 | |
sum1: -2572 sum2: -2572 t2 - t1 = 4.869097 t3 - t2 = 1.350818 | |
sum1: -2572 sum2: -2572 t2 - t1 = 4.826701 t3 - t2 = 1.371752 | |
sum1: -2572 sum2: -2572 t2 - t1 = 4.826821 t3 - t2 = 1.346859 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sum1: -2572 sum2: -2572 t2 - t1 = 2.283265 t3 - t2 = 0.490428
sum1: -2572 sum2: -2572 t2 - t1 = 2.282061 t3 - t2 = 0.489326
sum1: -2572 sum2: -2572 t2 - t1 = 2.281605 t3 - t2 = 0.480899
sum1: -2572 sum2: -2572 t2 - t1 = 2.282041 t3 - t2 = 0.489306
sum1: -2572 sum2: -2572 t2 - t1 = 2.279194 t3 - t2 = 0.490439
sum1: -2572 sum2: -2572 t2 - t1 = 2.286975 t3 - t2 = 0.488724
sum1: -2572 sum2: -2572 t2 - t1 = 2.282308 t3 - t2 = 0.480789
sum1: -2572 sum2: -2572 t2 - t1 = 2.284770 t3 - t2 = 0.517319
sum1: -2572 sum2: -2572 t2 - t1 = 2.280322 t3 - t2 = 0.480515
sum1: -2572 sum2: -2572 t2 - t1 = 2.289179 t3 - t2 = 0.488543