Guest User

Untitled

a guest
Feb 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define KILOBYTE 1024
  6.  
  7. static const unsigned long long ITERATIONS = 115;
  8. static const unsigned long long BUFFER_SIZE = 4 * KILOBYTE;
  9.  
  10.  
  11. int main() {
  12.     char buffer[ BUFFER_SIZE ];
  13.     char* name = "/tmp/somefile";
  14.     FILE* file = fopen( name, "r+");
  15.     long long size = 0;
  16.     long long read = 0;
  17.     long long random = 0;
  18.     long long i = 0;
  19.    
  20.     if( file == NULL ) return -1;
  21.    
  22.     fseek( file, 0, SEEK_END );
  23.     size = ftell( file );
  24.     rewind( file );
  25.    
  26.     if( size <= BUFFER_SIZE ) return -1;
  27.    
  28.     srand( time( NULL ) );
  29.  
  30.     for( ; i < ITERATIONS; ++i ) {
  31.         random = ( long long )rand() + ( long long )rand() * 0xFFFFFFFF;
  32.         random %= (size - BUFFER_SIZE);
  33.        
  34.         //if( ((random * 100) / size) < 40 ) {
  35.         //    --i;
  36.         //    continue;
  37.         //}
  38.            
  39.         fseek( file, random, SEEK_SET );
  40.         read = fread( buffer, 1, BUFFER_SIZE, file );
  41.         printf( "Reading 4KB from %lli, read %lli, as far as %lli perc.\n", random, read, (random * 100) / size );
  42.     }
  43.    
  44.     fclose( file );
  45.    
  46.     return 0;
  47. }
Add Comment
Please, Sign In to add comment