FreeBSD 5.4 RELEASE gcc 3.4.2 on Intel dual PIII 1Ghz

[postgres:~/develop/c/testbuf]$ gcc -Wall -o testbuf testbuf.c
[postgres:~/develop/c/testbuf]$ ./testbuf
duration round 1 of array method: 1.737 ms
duration round 2 of array method: 1.676 ms
duration round 3 of array method: 1.527 ms
duration round 1 of mul method: 0.548 ms
duration round 2 of mul method: 0.548 ms
duration round 3 of mul method: 0.546 ms
duration round 1 of shift method: 0.593 ms
duration round 2 of shift method: 0.592 ms
duration round 3 of shift method: 0.575 ms
[postgres:~/develop/c/testbuf]$ gcc -Wall -O2 -o testbuf testbuf.c
[postgres:~/develop/c/testbuf]$ ./testbuf
duration round 1 of array method: 0.169 ms
duration round 2 of array method: 0.165 ms
duration round 3 of array method: 0.165 ms
duration round 1 of mul method: 0.164 ms
duration round 2 of mul method: 0.164 ms
duration round 3 of mul method: 0.165 ms
duration round 1 of shift method: 0.164 ms
duration round 2 of shift method: 0.164 ms
duration round 3 of shift method: 0.165 ms

Looks to me like -O2 makes the difference very small (on this platform/gcc combo) - is 5/169 worth doing?

BTW - I patched the program to stop gcc whining:

--- testbuf.c.orig      Thu Aug 11 18:41:29 2005
+++ testbuf.c   Thu Aug 11 18:37:43 2005
@@ -2,6 +2,7 @@
  * testbuf.c
  */
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/file.h>
 #include <sys/param.h>
 #include <sys/stat.h>
@@ -66,6 +67,8 @@
        (long) (stop_t.tv_usec - start_t.tv_usec) % 1000);
   }
  }
+
+ return 0;
 }


Qingqing Zhou wrote:
"Tom Lane" <[EMAIL PROTECTED]> 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);
  }
 }
}



---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
       choose an index scan if your joining column's datatypes do not
       match




---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Reply via email to