![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| byte swapping 32-bit float and weird od results | GoDonkeys | UNIX for Advanced & Expert Users | 2 | 05-20-2009 08:54 PM |
| Help with __builtin_prefetch function and it's timing | Tavo | High Level Programming | 3 | 11-25-2008 05:31 AM |
| timing your functions | bebop1111116 | High Level Programming | 3 | 11-01-2006 04:19 PM |
| Timing out a SSH | rcunn87 | Shell Programming and Scripting | 9 | 07-31-2006 06:11 PM |
| scp timing out | jph | UNIX for Advanced & Expert Users | 1 | 06-09-2005 11:59 PM |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Weird timing results in C
I was running some timings in my code to see which of several functions was the best and I've been getting some odd results. Here's the code I'm using: Code:
static double time_loop(int (*foo)(int)) {
clock_t start, end;
int n = 0, i = 0;
start = clock();
for (; i <= MAXN; i++)
if ((*foo)(i)) n++;
end = clock();
if (n != 50847534)
return -1; // Error
return ((double) (end - start)) / CLOCKS_PER_SEC;
}
static double median3(int (*foo)(int)) {
double t1 = time_loop(foo);
double t2 = time_loop(foo);
double t3 = time_loop(foo);
printf(" ((%f %f %f))\n", t1, t2, t3);
int errors = 0;
if (t1 < 0)
errors++;
if (t2 < 0)
errors++;
if (t3 < 0)
errors++;
if (errors > 0)
return -errors;
if (t1 > t2) {
if (t2 > t3)
return t2;
return t3 < t1 ? t3 : t1;
} else {
if (t1 > t3)
return t1;
return t3 < t2 ? t3 : t2;
}
}
The functions that I'm passing to time_loop are pure functions -- they're not making any changes to global variables. And yet every time I run this, my debugging line in median3 shows that the third invocation takes significantly longer than the others. Sample run: Code:
((4.560000 3.750000 6.540000)) Original: 4.560000 s ((4.920000 3.760000 6.690000)) Original: 4.920000 s ((4.190000 3.750000 6.620000)) Original: 4.190000 s ((3.820000 3.130000 5.640000)) New: 3.820000 s ((3.580000 3.150000 5.580000)) New: 3.580000 s ((3.670000 3.140000 5.510000)) New: 3.670000 s Why is that? These functions are entirely deterministic, and nothing is changing between invocations. But I see a strong pattern of time - smaller time - much longer time. As for cache effects, the functions rely on a 125 MB global array, so that's not going to fit even in my 6MB L3. (Even if it did you'd expect the second and third invocations to be fast.) |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|