[vox-tech] benchmarking (again)
Kenneth Herron
kherron+lugod at fmailbox.com
Fri Feb 17 13:56:31 PST 2006
Peter Jay Salzman wrote:
> I was under the impression that this is what computer scientists call
> "locality of data" -- if data is used once, it'll be used again soon. The
> strategy that attempts to exploit locality of data is to hold recent data in
> L1 cache.
The cache isn't under the programmer's direct control. It's a function
of the hardware. The best the programmer can usually do is to try to
access memory in ways that lets the cache work well. For example:
int array1[10000], array2[10000], array3[10000];
/* Version 1 */
for (i = 0; i < 10000; i++) {
sum1 += array1[i];
sum2 += array2[i];
sum3 += array3[i];
}
/* Version 2 */
for (i = 0; i < 10000; i++)
sum1 += array1[i];
for (i = 0; i < 10000; i++)
sum2 += array2[i];
for (i = 0; i < 10000; i++)
sum3 += array3[i];
You might expect version 1 to run faster because it has only one set of
loop increment operations. But version 1 jumps among three widely
separated memory blocks on each iteration, while version 2 accesses
memory in a simple linear fashion. The latter is more cache-friendly. It
might even run faster.
> I see what you mean. Yeah, but the question still remains -- what's going
> on with the user time in java apps, and why does the program run faster in
> Java than C++? Certainly C++ compilers know how to do all that stuff too.
The code you're benchmarking doesn't have any side effects. Maybe the
java compiler was smart enough to optimize the whole thing down to an
empty function :-)
More information about the vox-tech
mailing list