Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/time.h>
- int main(int argc, char** argv)
- {
- int bufsize;
- int numiter;
- int lcv;
- struct timeval start_tv;
- struct timeval end_tv;
- double delta;
- char* buf1;
- char* buf2;
- if ( argc < 2 )
- {
- printf("Usage: %s <buffer size in bytes> <number of iterations>\ n", argv[0]);
- exit(1);
- }
- bufsize = atoi(argv[1]);
- numiter = atoi(argv[2]);
- if ( bufsize <= 0 || numiter <= 0 )
- {
- printf("Usage: %s <buffer size in bytes> <number of iterations>\ n", argv[0]);
- exit(1);
- }
- buf1 = malloc(sizeof(char) * bufsize);
- buf2 = malloc(sizeof(char) * bufsize);
- if ( buf1 == NULL || buf2 == NULL )
- {
- printf("Error, cannot malloc enough space for the test\n");
- exit(1);
- }
- memset((void*) buf1, 'A', bufsize);
- gettimeofday(&start_tv, NULL);
- for ( lcv = 0; lcv < numiter; lcv++ )
- {
- memcpy(buf2, buf1, bufsize);
- memcpy(buf1, buf2, bufsize);
- }
- gettimeofday(&end_tv, NULL);
- delta = end_tv.tv_sec - start_tv.tv_sec +
- ( end_tv.tv_usec - start_tv.tv_usec ) / 1000000.0;
- printf("memcpy took %f seconds\n", delta);
- gettimeofday(&start_tv, NULL);
- for ( lcv = 0; lcv < numiter; lcv++ )
- {
- memmove(buf2, buf1, bufsize);
- memmove(buf1, buf2, bufsize);
- }
- gettimeofday(&end_tv, NULL);
- delta = end_tv.tv_sec - start_tv.tv_sec +
- ( end_tv.tv_usec - start_tv.tv_usec ) / 1000000.0;
- printf("memmove took %f seconds\n", delta);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment