#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define TRIALS 10
#define MAXCOUNT 1000000 // number of doubles to allocate for
double * calloc_d(size_t nmemb);
double * calloc_d2(size_t nmemb);
int main(void)
{
struct timespec before, after;
double stupidsum = 0.0;
double *dbl_stores[TRIALS];
double *dbl_stores2[TRIALS];
int trials;
// Let them warm up
trials = TRIALS;
while(trials--){
dbl_stores[trials] = calloc_d(MAXCOUNT);
dbl_stores2[trials] = calloc_d2(MAXCOUNT);
free(dbl_stores
[trials
]); free(dbl_stores2
[trials
]); }
// Time old code
timespec_get(&before, TIME_UTC);
trials = TRIALS;
while(trials--){
dbl_stores[trials] = calloc_d(MAXCOUNT);
}
timespec_get(&after, TIME_UTC);
long dur = (after.tv_sec - before.tv_sec)*1e9 + (after.tv_nsec - before.tv_nsec);
printf("Old code took %ld ns\n", dur
);
// Time simple code
timespec_get(&before, TIME_UTC);
trials = TRIALS;
while(trials--){
dbl_stores2[trials] = calloc_d2(MAXCOUNT);
}
timespec_get(&after, TIME_UTC);
dur = (after.tv_sec - before.tv_sec)*1e9 + (after.tv_nsec - before.tv_nsec);
printf("Simple code took %ld ns\n", dur
);
trials = TRIALS;
while(trials--){
for(int i = 0; i < MAXCOUNT; ++i){
stupidsum += dbl_stores[trials][i] + dbl_stores2[trials][i];
}
}
printf("print sum to avoid code under test being optimized out: %lf\n", stupidsum
);
// Yeah I leak the buffers, so sue me. Doesn't affect the benchmark.
return 0;
}
double * calloc_d2(size_t nmemb)
{
double* ret
= malloc(sizeof(double)*nmemb
);
if(ret){
double* i = ret;
double* end = i + nmemb;
while(i != end){
*i = 0.0;
++i;
}
}
return ret;
}
double * calloc_d(size_t nmemb)
{
double *ret;
double *next; // pointer to beginning of uninitialized segment
size_t alloc_bytes = sizeof(*ret) * nmemb;
size_t init_sz; // size of initialized segment
next = ret;
if (ret) {
ret[0] = 0.0;
init_sz = sizeof(*ret);
++next;
while (init_sz < (alloc_bytes + sizeof(*ret)) / 2) {
init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JuIDA7Cn0KCmRvdWJsZSAqIGNhbGxvY19kMihzaXplX3Qgbm1lbWIpCnsKICAgIGRvdWJsZSogcmV0ID0gbWFsbG9jKHNpemVvZihkb3VibGUpKm5tZW1iKTsKICAgIAogICAgaWYocmV0KXsKICAgICAgICBkb3VibGUqIGkgPSByZXQ7CiAgICAJZG91YmxlKiBlbmQgPSBpICsgbm1lbWI7CiAgICAJd2hpbGUoaSAhPSBlbmQpewogICAgCQkqaSA9IDAuMDsKICAgIAkJKytpOwoJCX0KICAgIH0KICAgIHJldHVybiByZXQ7Cn0KCmRvdWJsZSAqIGNhbGxvY19kKHNpemVfdCBubWVtYikKewogICAgZG91YmxlICpyZXQ7CiAgICBkb3VibGUgKm5leHQ7ICAgICAgICAgLy8gcG9pbnRlciB0byBiZWdpbm5pbmcgb2YgdW5pbml0aWFsaXplZCBzZWdtZW50CiAgICBzaXplX3QgYWxsb2NfYnl0ZXMgPSBzaXplb2YoKnJldCkgKiBubWVtYjsKICAgIHNpemVfdCBpbml0X3N6OyAgICAgICAvLyBzaXplIG9mIGluaXRpYWxpemVkIHNlZ21lbnQKCiAgICByZXQgPSBtYWxsb2MoYWxsb2NfYnl0ZXMpOwogICAgbmV4dCA9IHJldDsKCiAgICBpZiAocmV0KSB7CiAgICAgICAgcmV0WzBdID0gMC4wOwogICAgICAgIGluaXRfc3ogPSBzaXplb2YoKnJldCk7CiAgICAgICAgKytuZXh0OwoKICAgICAgICB3aGlsZSAoaW5pdF9zeiA8IChhbGxvY19ieXRlcyArIHNpemVvZigqcmV0KSkgLyAyKSB7CiAgICAgICAgICAgIG1lbW1vdmUobmV4dCwgcmV0LCBpbml0X3N6KTsKICAgICAgICAgICAgaW5pdF9zeiAqPSAyOwogICAgICAgICAgICBuZXh0ID0gcmV0ICsgaW5pdF9zeiAvIHNpemVvZigqcmV0KTsgICAgCiAgICAgICAgfQogICAgICAgIG1lbW1vdmUobmV4dCwgcmV0LCBhbGxvY19ieXRlcyAtIGluaXRfc3opOwogICAgfQoKICAgIHJldHVybiByZXQ7Cn0=