/* * memcpytest.c: Attempt to measure performance of memcpy. */ #include #include #include #include #include #include int main(int argc, char **argv) { int blocksz; /* The block size to copy. */ int numblocks; /* How many copies to do. */ char *buf; /* Input buffer. */ typedef struct _BlockArray { char *blockentry; } BlockArray; BlockArray *blockarray; /* Array of block entry ptrs. */ int i; struct timeval starttp, endtp, elapsedtp; double elapsed; if (argc != 3) { printf("usage %s blocksize num_blocks\n", argv[0]); exit(1); } else { blocksz = atoi(argv[1]); numblocks = atoi(argv[2]); } /* Start timing setup. */ gettimeofday(&starttp, NULL); /* Allocate source buffer and initialize to something trivial. */ buf = (char *) malloc(blocksz); if (buf == NULL) { printf("out of memory initializing source buffer!\n"); exit(2); } memset(buf, 1, blocksz); /* Allocate destination array of buffer pointers. */ blockarray = malloc(numblocks * sizeof(blockarray)); if (blockarray == NULL) { printf("out of memory initializing destination array!\n"); exit(2); } /* Allocate a block entry for each element of blockarray. */ for (i = 0; i < numblocks; i++) { blockarray[i].blockentry = malloc(blocksz); if (blockarray[i].blockentry == NULL) { printf("out of memory initializing destination array contents!\n"); exit(2); } } gettimeofday(&endtp, NULL); timersub(&endtp, &starttp, &elapsedtp); elapsed = (double)elapsedtp.tv_sec + (double)elapsedtp.tv_usec/1000000.0; printf("%d malloc in %.4fs\n", numblocks, elapsed); /* Start timing copy now. */ gettimeofday(&starttp, NULL); /* Perform the copy. */ for (i = 0; i < numblocks; i++) { memcpy((void *)blockarray[i].blockentry, (void *)buf, blocksz); } gettimeofday(&endtp, NULL); timersub(&endtp, &starttp, &elapsedtp); elapsed = (double)elapsedtp.tv_sec + (double)elapsedtp.tv_usec/1000000.0; printf("%d memcpy of %d byte blocks in %.4fs\n", numblocks, blocksz, elapsed); free(buf); for (i = 0; i < numblocks; i++) { free(blockarray[i].blockentry); } free(blockarray); exit(0); }