Last active
December 12, 2015 11:49
-
-
Save pt300/da349c78aba860e33cec to your computer and use it in GitHub Desktop.
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
#include <stdio.h> | |
#include <time.h> | |
#include <stdlib.h> | |
#include "pcg/pcg_basic.h" | |
struct timespec diff_t(struct timespec, struct timespec); | |
int main(void) { | |
pcg32_srandom(time(NULL) ^ (intptr_t)&printf, rand()); | |
struct timespec start, end; | |
long long int diff; | |
int a; | |
int rand[200000]; | |
a = 200000; | |
while(a--) { | |
rand[a] = (int)pcg32_boundedrand(1000); | |
} | |
char* a1 = malloc(1000*1000); | |
char** a2 = malloc(1000); | |
a = 1000; | |
while(a--) { | |
a2[a] = malloc(1000); | |
} | |
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); | |
// 2D array of 1000 x 1000 elements. | |
int c; | |
a = 200000; | |
while(a-->0) { | |
c = a1[rand[a--]*1000+rand[a]]; | |
c+=2; | |
} | |
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end); | |
diff = (diff_t(end, start)).tv_nsec; | |
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); | |
// Jagged array of 1000 x 1000 elements. | |
a = 200000; | |
while(a-->0) { | |
c = a2[rand[a--]][rand[a]]; | |
c+=2; | |
} | |
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end); | |
printf("Perf:\n%llins normal array\n%llins jagged array\n", diff, (long long int)(diff_t(end, start)).tv_nsec); | |
return EXIT_SUCCESS; | |
} | |
struct timespec diff_t(struct timespec end, struct timespec start) | |
{ | |
struct timespec temp; | |
if ((end.tv_nsec-start.tv_nsec)<0) { | |
temp.tv_sec = end.tv_sec-start.tv_sec-1; | |
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; | |
} else { | |
temp.tv_sec = end.tv_sec-start.tv_sec; | |
temp.tv_nsec = end.tv_nsec-start.tv_nsec; | |
} | |
return temp; | |
} |
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
With compiler optimizations: | |
Perf: | |
908ns normal array | |
333ns jagged array | |
Perf: | |
419ns normal array | |
138ns jagged array | |
Perf: | |
454ns normal array | |
140ns jagged array | |
Without: | |
Perf: | |
517845ns normal array | |
582495ns jagged array | |
Perf: | |
1255826ns normal array | |
1478993ns jagged array | |
Perf: | |
979111ns normal array | |
1466433ns jagged array | |
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
With optimizations: | |
Perf: | |
580ns normal array | |
225ns jagged array | |
Perf: | |
652ns normal array | |
225ns jagged array | |
Perf: | |
1062ns normal array | |
716ns jagged array | |
Without: | |
Perf: | |
5775760ns normal array | |
2605375ns jagged array | |
Perf: | |
5751747ns normal array | |
6772963ns jagged array | |
Perf: | |
4785553ns normal array | |
2549643ns jagged array |
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
#include <stdio.h> | |
#include <time.h> | |
#include <stdlib.h> | |
struct timespec diff_t(struct timespec, struct timespec); | |
int main(void) { | |
struct timespec start, end; | |
long long int diff; | |
int a, x, tmp1 = 0; | |
char a1[1000*1000]; | |
char* a2[1000]; | |
a = 1000; | |
while(a--) { | |
a2[a] = malloc(1000); | |
} | |
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); | |
// 2D array of 1000 x 1000 elements. | |
for (a = 0; a < 1000; a++) { | |
for (x = 0; x < 1000; x++) { | |
int c = a1[tmp1+x]; | |
} | |
tmp1 += 1000; | |
} | |
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end); | |
diff = (diff_t(end, start)).tv_nsec; | |
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); | |
// Jagged array of 100 x 100 elements. | |
for (a = 0; a < 1000; a++) { | |
for (x = 0; x < 1000; x++) { | |
int c = a2[a][x]; | |
} | |
} | |
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end); | |
printf("Perf:\n%llins normal array\n%llins jagged array\n", diff, (long long int)(diff_t(end, start)).tv_nsec); | |
return EXIT_SUCCESS; | |
} | |
struct timespec diff_t(struct timespec end, struct timespec start) | |
{ | |
struct timespec temp; | |
if ((end.tv_nsec-start.tv_nsec)<0) { | |
temp.tv_sec = end.tv_sec-start.tv_sec-1; | |
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; | |
} else { | |
temp.tv_sec = end.tv_sec-start.tv_sec; | |
temp.tv_nsec = end.tv_nsec-start.tv_nsec; | |
} | |
return temp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment