A typical approach is to record time()
twice and run the code many times to get a 1st order approximation.
time_t t0,t1;
time(&t0);
int N = 1000000;
for (int i=0; i< N; i++) {
DoCodeUnderTest();
// c.real = a.real + b.real;
// c.img = a.img + b.img;
}
time(&t1);
printf("Time %e\n", (double) (t1-t0) / N);
Suggest using profiling tools for a more accurate answer.
@Jonathan Leffler suggestion to use clock()
is also an improvement.
clock_t c1,c12;
c1 = clock();
... // repeat runs of the code
c2 = clock();
printf("Time %e\n", (double) (c1-c0) / CLOCKS_PER_SEC / N);
Given 1) @Jonathan Leffler 2nd suggestion about the legitimacy of repeating since a compiler may out-think the code and 2) cache issues hints that any brute force methods as suggested here are at best illustrative and not definitive time measurements.