Loading lang_c_ex_1201...
double sum(const double *values, int valueCount) { double sum=0.0; for(int i=0; i<valueCount; ++i) { sum+=values[i]; } return sum; }
int halfValues(const double *values, int valueCount) { int split=-1; double partialSum=0.0; for(int i=0; i<valueCount; ++i) { partialSum+=values[i]; if(partialSum>0.5*sum(values, valueCount)) { break; } split=i; } return split; }
#include <sys/time.h>
double // seconds (1e-6 precision) since 1970/01/01 00:00:00 UTC now(void) { struct timeval tv; gettimeofday(&tv, NULL); return (double)tv.tv_sec+1e-6*(double)tv.tv_usec; }
void test_halfValues(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const int valueCount=10000; double *values=(double *)malloc(valueCount*sizeof(double)); if(values==NULL) { abort(); } for(int i=0; i<valueCount; ++i) { const double value=i*0.01; values[i]=value*value; } const double t0=now(); double duration=0.0; int result, iterCount; for(iterCount=0; duration<5.0; ++iterCount, duration=now()-t0) { result=halfValues(values, valueCount); } printf("result=%d --> %g it/s\n", result, iterCount/duration); free(values); }
double mean(const double *values, int valueCount) { double sum=0.0; for(int i=0; i<valueCount; ++i) { sum+=values[i]; } return sum/valueCount; }
double variance(const double *values, int valueCount) { double sqrSum=0.0; for(int i=0; i<valueCount; ++i) { const double delta=values[i]-mean(values, valueCount); sqrSum+=delta*delta; } return sqrSum/valueCount; }
#include <sys/time.h>
double // seconds (1e-6 precision) since 1970/01/01 00:00:00 UTC now(void) { struct timeval tv; gettimeofday(&tv, NULL); return (double)tv.tv_sec+1e-6*(double)tv.tv_usec; }
void test_variance(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const int valueCount=10000; double *values=(double *)malloc(valueCount*sizeof(double)); if(values==NULL) { abort(); } for(int i=0; i<valueCount; ++i) { const double value=i*0.01; values[i]=value*value; } const double t0=now(); double result, duration=0.0; int iterCount; for(iterCount=0; duration<5.0; ++iterCount, duration=now()-t0) { result=variance(values, valueCount); } printf("result=%g --> %g it/s\n", result, iterCount/duration); free(values); }
//----------------------------------------------------------------------------
#ifndef PERFORMANCES_H #define PERFORMANCES_H
double sum(const double *values, int valueCount);
int halfValues(const double *values, int valueCount);
double mean(const double *values, int valueCount);
double variance(const double *values, int valueCount);
#endif // PERFORMANCES_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "performances.h"
#include <assert.h>
#define INITIAL_VERSION 0
double sum(const double *values, int valueCount) { double sum=0.0; for(int i=0; i<valueCount; ++i) { sum+=values[i]; } return sum; }
int halfValues(const double *values, int valueCount) { assert(valueCount>0); // detect incorrect parameter int split=-1; double partialSum=0.0; #if INITIAL_VERSION for(int i=0; i<valueCount; ++i) { partialSum+=values[i]; if(partialSum>0.5*sum(values, valueCount)) { break; } split=i; } #else // prevent multiple useless evaluations const double halfTotalSum=0.5*sum(values, valueCount); for(int i=0; i<valueCount; ++i) { partialSum+=values[i]; if(partialSum>halfTotalSum) { break; } split=i; } #endif return split; }
double mean(const double *values, int valueCount) { double sum=0.0; for(int i=0; i<valueCount; ++i) { sum+=values[i]; } return sum/valueCount; }
double variance(const double *values, int valueCount) { assert(valueCount>0); // detect incorrect parameter double sqrSum=0.0; #if INITIAL_VERSION for(int i=0; i<valueCount; ++i) { const double delta=values[i]-mean(values, valueCount); sqrSum+=delta*delta; } #else // prevent multiple useless evaluations const double meanValue=mean(values, valueCount); for(int i=0; i<valueCount; ++i) { const double delta=values[i]-meanValue; sqrSum+=delta*delta; } #endif return sqrSum/valueCount; }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "performances.h"
#include <stdio.h> #include <stdlib.h> #include <sys/time.h>
#define INTENTIONAL_BUG 0
double // seconds (1e-6 precision) since 1970/01/01 00:00:00 UTC now(void) { struct timeval tv; gettimeofday(&tv, NULL); return (double)tv.tv_sec+1e-6*(double)tv.tv_usec; }
void test_halfValues(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const int valueCount=10000; double *values=(double *)malloc(valueCount*sizeof(double)); if(values==NULL) { abort(); } for(int i=0; i<valueCount; ++i) { const double value=i*0.01; values[i]=value*value; } const double t0=now(); double duration=0.0; int result, iterCount; for(iterCount=0; duration<5.0; ++iterCount, duration=now()-t0) { #if INTENTIONAL_BUG // artificially generate an assertion failure // in order to illustrate assert() result=halfValues(values, 0); #else // normal computation result=halfValues(values, valueCount); #endif } printf("result=%d --> %g it/s\n", result, iterCount/duration); free(values); }
void test_variance(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const int valueCount=10000; double *values=(double *)malloc(valueCount*sizeof(double)); if(values==NULL) { abort(); } for(int i=0; i<valueCount; ++i) { const double value=(i%1000)*0.01; values[i]=value*value; } const double t0=now(); double result, duration=0.0; int iterCount; for(iterCount=0; duration<5.0; ++iterCount, duration=now()-t0) { #if INTENTIONAL_BUG // artificially generate an assertion failure // in order to illustrate assert() result=variance(values, 0); #else // normal computation result=variance(values, valueCount); #endif } printf("result=%g --> %g it/s\n", result, iterCount/duration); free(values); }
int main(void) { test_halfValues(); test_variance(); return 0; }
//----------------------------------------------------------------------------