/*
 * Attempt to test memory copy speeds.  Use a buffer large enough to
 * defeat the on-cpu L1 and L2 caches.
 */

#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>

#define NLOOP	100

char Buf1[2 * 1024 * 1024];
char Buf2[2 * 1024 * 1024];

int deltausecs(struct timeval *tv1, struct timeval *tv2);

int
main(int ac, char **av)
{
    int i;
    double dtime;
    struct timeval tv1;
    struct timeval tv2;

	/* Test memcopy() */
    memset(Buf1, 1, sizeof(Buf1));
    for (i = 0; i < 10; ++i)
		memcpy(Buf1, Buf2, sizeof(Buf1));

    gettimeofday(&tv1, NULL);
    for (i = 0; i < NLOOP; ++i)
		memcpy(Buf1, Buf2, sizeof(Buf1));
    gettimeofday(&tv2, NULL);

    dtime = (double)deltausecs(&tv1, &tv2);
    printf("memcpy():%6.2f MBytes/sec (copy)\n", (double)sizeof(Buf1) * NLOOP / dtime);

	return(0);
}

int
deltausecs(struct timeval *tv1, struct timeval *tv2)
{
    int usec;

    usec = (tv2->tv_usec + 1000000 - tv1->tv_usec);
    usec += (tv2->tv_sec - tv1->tv_sec - 1) * 1000000;
    return(usec);
}

