"Tom Lane" <tgl@sss.pgh.pa.us> writes:
>
> Also, I would like to see the actual test code. I wonder whether what
> you measured is the ability of the compiler to optimize references to
> successive elements of an array inside a loop; that has little or
> nothing to do with the typical usage of BufferGetBlock().
>
The source code is attached.
compiled with "gcc testbuf.c". I tried -O2 actually, and it turns out that
the timing is reduced a lot so not believable.
---
/*
* testbuf.c
*/
#include <stdio.h>
#include <sys/file.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#define BLCKSZ 8192
#define NBuffers 80000
typedef void* Block;
int main(void)
{
int i, round, method;
Block k, start;
struct timeval start_t, stop_t;
long usecs;
Block *array = (Block *) calloc(NBuffers, sizeof(Block));
start = (Block)0xff3386;
for (i = 0; i < NBuffers; i++)
array[i] = start + BLCKSZ*i;
for (method = 0; method < 3; method ++)
{
start = (Block)0xff3386;
for (round = 0; round < 3; round ++)
{
gettimeofday(&start_t, NULL);
if (method == 0)
{
for (i = 0; i < NBuffers; i++)
k = array[i];
}
if (method == 1)
{
for (i = 0; i < NBuffers; i++)
k = start + i*BLCKSZ;
}
if (method == 2)
{
for (i = 0; i < NBuffers; i++)
k = start + (i<<13);
}
gettimeofday(&stop_t, NULL);
if (stop_t.tv_usec < start_t.tv_usec)
{
stop_t.tv_sec--;
stop_t.tv_usec += 1000000;
}
usecs = (long) (stop_t.tv_sec - start_t.tv_sec) * 1000000
+ (long) (stop_t.tv_usec - start_t.tv_usec);
fprintf (stdout, "duration round %d of %s method: %ld.%03ld ms\n",
round + 1,
method==0?"array":method==1?"mul":"shift",
(long) ((stop_t.tv_sec - start_t.tv_sec) * 1000 +
(stop_t.tv_usec - start_t.tv_usec) / 1000),
(long) (stop_t.tv_usec - start_t.tv_usec) % 1000);
}
}
}