1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
int main(int argc, char *argv[])
{
//File to read
FILE* fichierR = NULL;
char* fileNameR = "./file.txt";
//Useful to get the file size
struct stat buf;
stat(fileNameR, &buf);
float size = buf.st_size;
printf("file size = %.0f bytes\n",size);
char buffer[8192]; //8Ko of buffer
int v = 0;
struct timeval tv1, tv2;
float duration, readSpeed;
//open read only
fichierR = fopen(fileNameR,"r");
if(fichierR != NULL){
gettimeofday(&tv1, NULL); //get the current time
do{
//read 8191 bytes of the file and store them into buffer
v = fread(buffer, 1, 8191, fichierR);
}while(v != 0);
gettimeofday(&tv2, NULL);
//Compute the time to read the file
duration = (float) (tv2.tv_usec - tv1.tv_usec)/1000000 + (float) (tv2.tv_sec - tv1.tv_sec);
//Compute the read speed
readSpeed = (size*8)/duration;
printf ("Read time = %.15f seconds\n", duration);
printf ("Read speed = %f Gb/s\n", readSpeed/1000000000);
}else{
printf("Error during opening file %s\n",fileNameR);
}
fclose(fichierR);
return 0;
} |
Partager