[postgis-devel] Cost of memcmp

Paul Ramsey pramsey at cleverelephant.ca
Mon Oct 6 21:59:08 PDT 2008


I can't see anything wrong with this test program, but it seems to
indicate that the cost of memcmp is basically constant and tiny.
Anyone see what I'm doing wrong? If the answer is "no", then we should
clearly by moving to a memcmp implementation.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

clock_t start_time, end_time;
int i, j;
int loop = 10000000;
size_t size = 1024;
char* memseg1;
char* memseg2;

int expn = 1;

int main() {

  printf("%d ticks per second.\n", CLOCKS_PER_SEC);

  for(j = 0; j < 18; j++ ) {

    memseg1 = malloc(size);
    memseg2 = malloc(size);
    memcpy(memseg2, memseg1, size);

    start_time = clock();
    for( i = 0; i < loop; i++ )
    {
      if(memcmp(memseg1, memseg2, size)) {}
    }
    end_time = clock();

    printf("Compared %d bytes, %d times, took %d ticks.\n", size,
loop, end_time - start_time);
    size = size * 2;
    free(memseg1);
    free(memseg2);
  }
}



More information about the postgis-devel mailing list