see code from programming pearls
http://www.cs.bell-labs.com/cm/cs/pearls/timemod.c
and also my effort to do myself
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#define maxn 100000
int x[maxn];
int startn=5000;
int n;
//function to be timed
int intcmp(int *i,int *j)
{ return *i-*j;}
#define swapmac(i,j) {t=x[i];x[i]=x[j];x[j]=t;}
void swapfunc(int i,int j){
int t=x[i];
x[i]=x[j];
x[j]=t;
}
#define maxmac(a,b) ((a)>(b)?(a):(b));
int maxfunc(int a,int b){
return a>b?a:b;
}
#define T(s) printf("%s (n=%d)\n",s,n);
#define trials 5
#define M(op) \
printf(" %-22s",&op); \
k=0; \
timesum=0; \
for (ex=0;ex<trials;ex++) { \
start=clock(); \
for (i=1;i<=n;i++) { \
fi=(float) i; \
for (j=1;j<=n;j++) { \
op; \
} \
} \
t=clock()-start; \
printf("%6d",t); \
timesum+=t; \
} \
nans=1e9 * timesum/ ((double)\
n*n*trials*CLOCKS_PER_SEC);\
printf("8.0f\n".nans);
int main(){
int i,j,k;
float fi,fj,fk;
int t,ex,timesum,start,globalstart;
double nans;
globalstart=clock();
for (int i=0;i<n;i++)
x[i]=rand();
n=startn;
printf(" C Time Cost Model,n=%d\n",n);
T("Integer Arithmetic");
M({});
M(k++);
M(k = i + j);
M(k = i - j);
M(k = i * j);
M(k = i / j);
M(k = i % j);
M(k = i & j);
M(k = i | j);
T("Floating Point Arithmetic");
M(fj=j;);
M(fj=j; fk = fi + fj;);
M(fj=j; fk = fi - fj;);
M(fj=j; fk = fi * fj;);
M(fj=j; fk = fi / fj;);
T("Array Operations");
M(k = i + j);
M(k = x[i] + j);
M(k = i + x[j]);
M(k = x[i] + x[j]);
T("Comparisons");
M(if (i < j) k++);
M(if (x[i] < x[j]) k++);
T("Array Comparisons and Swaps");
M(k = (x[i]<x[k]) ? -1:1);
M(k = intcmp(x+i, x+j));
M(swapmac(i, j));
M(swapfunc(i, j));
T("Max Function, Macro and Inline");
M(k = (i > j) ? i : j);
M(k = maxmac(i, j));
M(k = maxfunc(i, j));
n = startn / 5;
T("Math Functions");
M(fk = j+fi;);
M(k = rand(););
M(fk = sqrt(j+fi));
M(fk = sin(j+fi));
M(fk = sinh(j+fi));
M(fk = asin(j+fi));
M(fk = cos(j+fi));
M(fk = tan(j+fi));
n = startn / 10;
T("Memory Allocation");
M(free(malloc(16)););
M(free(malloc(100)););
M(free(malloc(2000)););
/* Possible additions: input, output, malloc */
printf(" Secs: %4.2f\n",
((double) clock()-globalstart)
/ CLOCKS_PER_SEC);
return 0;
}
list of mistakes
1
>------ Build started: Project: run_time, Configuration: Debug Win32 ------
1> run.cpp
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(56): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(56): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(56): error C2143: syntax error : missing ';' before '{'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(56): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(56): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(57): error C2102: '&' requires l-value
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(57): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(58): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(58): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(59): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(59): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(60): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(60): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(61): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(61): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(62): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(62): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(63): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(63): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(64): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(64): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(66): error C2440: '=' : cannot convert from 'int' to 'float *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(66): error C2143: syntax error : missing ')' before ';'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(66): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(66): warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(66): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(67): error C2440: '=' : cannot convert from 'int' to 'float *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(67): error C2143: syntax error : missing ')' before ';'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(67): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(67): warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(67): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(68): error C2440: '=' : cannot convert from 'int' to 'float *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(68): error C2143: syntax error : missing ')' before ';'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(68): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(68): warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(68): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(69): error C2440: '=' : cannot convert from 'int' to 'float *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(69): error C2143: syntax error : missing ')' before ';'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(69): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(69): warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(69): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(70): error C2440: '=' : cannot convert from 'int' to 'float *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(70): error C2143: syntax error : missing ')' before ';'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(70): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(70): warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(70): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(72): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(72): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(73): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(73): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(74): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(74): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(75): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(75): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(77): error C2059: syntax error : 'if'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(77): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(78): error C2059: syntax error : 'if'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(78): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(80): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(80): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(81): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(81): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(82): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(82): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(82): error C2143: syntax error : missing ';' before '{'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(82): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(82): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(83): error C2102: '&' requires l-value
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(83): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(85): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(85): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(86): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(86): error C2143: syntax error : missing ')' before ';'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(86): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(86): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(87): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(87): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(90): error C2440: '=' : cannot convert from 'float' to 'float *'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(90): error C2143: syntax error : missing ')' before ';'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(90): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(90): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(91): error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(91): error C2143: syntax error : missing ')' before ';'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(91): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(91): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(92): error C2440: '=' : cannot convert from 'float' to 'float *'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(92): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(93): error C2440: '=' : cannot convert from 'float' to 'float *'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(93): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(94): error C2440: '=' : cannot convert from 'float' to 'float *'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(94): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(95): error C2440: '=' : cannot convert from 'float' to 'float *'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(95): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(96): error C2440: '=' : cannot convert from 'float' to 'float *'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(96): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(97): error C2440: '=' : cannot convert from 'float' to 'float *'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(97): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(100): error C2102: '&' requires l-value
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(100): error C2143: syntax error : missing ')' before ';'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(100): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(100): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(101): error C2102: '&' requires l-value
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(101): error C2143: syntax error : missing ')' before ';'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(101): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(101): error C2228: left of '.nans' must have class/struct/union
1> type is 'const char [6]'
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(102): error C2102: '&' requires l-value
1>c:\users\david\documents\visual studio 2010\projects\run_time\run.cpp(102): fatal error C1003: error count exceeds 100; stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========