Guest User

memtest.c

a guest
Mar 10th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/time.h>
  5.  
  6. int main(int argc, char** argv)
  7. {
  8.         int bufsize;
  9.         int numiter;
  10.         int lcv;
  11.  
  12.         struct timeval  start_tv;
  13.         struct timeval  end_tv;
  14.         double delta;
  15.  
  16.         char* buf1;
  17.         char* buf2;
  18.  
  19.         if ( argc < 2 )
  20.         {
  21.                 printf("Usage: %s <buffer size in bytes> <number of iterations>\                                              n", argv[0]);
  22.                 exit(1);
  23.         }
  24.  
  25.         bufsize = atoi(argv[1]);
  26.         numiter = atoi(argv[2]);
  27.  
  28.         if ( bufsize <= 0 || numiter <= 0 )
  29.         {
  30.                 printf("Usage: %s <buffer size in bytes> <number of iterations>\                                              n", argv[0]);
  31.                 exit(1);
  32.         }
  33.  
  34.         buf1 = malloc(sizeof(char) * bufsize);
  35.         buf2 = malloc(sizeof(char) * bufsize);
  36.  
  37.         if ( buf1 == NULL || buf2 == NULL )
  38.         {
  39.                 printf("Error, cannot malloc enough space for the test\n");
  40.                 exit(1);
  41.         }
  42.  
  43.         memset((void*) buf1, 'A', bufsize);
  44.  
  45.         gettimeofday(&start_tv, NULL);
  46.         for ( lcv = 0; lcv < numiter; lcv++ )
  47.         {
  48.                 memcpy(buf2, buf1, bufsize);
  49.                 memcpy(buf1, buf2, bufsize);
  50.         }
  51.         gettimeofday(&end_tv, NULL);
  52.         delta = end_tv.tv_sec - start_tv.tv_sec +
  53.                 ( end_tv.tv_usec - start_tv.tv_usec ) / 1000000.0;
  54.  
  55.         printf("memcpy took %f seconds\n", delta);
  56.  
  57.         gettimeofday(&start_tv, NULL);
  58.         for ( lcv = 0; lcv < numiter; lcv++ )
  59.         {
  60.                 memmove(buf2, buf1, bufsize);
  61.                 memmove(buf1, buf2, bufsize);
  62.         }
  63.         gettimeofday(&end_tv, NULL);
  64.         delta = end_tv.tv_sec - start_tv.tv_sec +
  65.                 ( end_tv.tv_usec - start_tv.tv_usec ) / 1000000.0;
  66.  
  67.         printf("memmove took %f seconds\n", delta);
  68.  
  69.         return 0;
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment