[ create a new paste ] login | about

Link: http://codepad.org/rLCqCOOQ    [ raw code | output | fork ]

aaronla - C, pasted on Jan 12:
#include <time.h>
#include <sys/times.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>

#define ITER 10000
#define N 1024

double gettime() {
	struct timeval time;
	double t = 0;
	gettimeofday(&time, NULL);
	t = (double)time.tv_sec;
	t += (double)time.tv_usec / (double)1000000;
	return t;
}

double test(void (*fn)(int n))
{
  double t0, t1;
  fn(10);
  t0 = gettime();
  fn(ITER);
  t1 = gettime();
  return t1 - t0;
}

char buf1[N];
char buf2[N];

__attribute__ ((noinline)) void memcpy_(void* d, void* s, size_t n) {
  memcpy(d, s, n);
}
__attribute__ ((noinline)) void strcpy_(char* d, char* s) {
  strcpy(d, s);
}

void tmemcpy(int n)
{
  while(n--)
  {
    memcpy_(buf2, buf1, N);
    memcpy_(buf1, buf2, N);
  }
}

void tstrcpy(int n)
{
  while(n--)
  {
    strcpy_(buf2, buf1);
    strcpy_(buf1, buf2);
  }
}

int main()
{
  double tm, ts;
  int i;
  for (i = 0; i < N-1; ++i)
  {
    buf1[i] = 'a';
  }
  buf1[N-1] = '\0';

  tm = test(tmemcpy);
  ts = test(tstrcpy);
  printf("memcpy time: %g\n", tm);
  printf("strcpy time: %g\n", ts);
  return 0;
}


Output:
1
2
memcpy time: 0.00176589
strcpy time: 0.0913031


Create a new paste based on this one


Comments: