#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#define BUFFERSIZE (60000)
#define MEGABYTE (1024*1024)
int main(int argc, char * argv[]){
long megabytes,i;
int fo;
char buffer[BUFFERSIZE];
clock_t t;
for (i=0; i < BUFFERSIZE ; i++)
buffer[i]='A'; //tao du lieu tren buffer de sau day vao file
fo = open(argv[1], O_CREAT | O_WRONLY | O_SYNC);
megabytes=atol(argv[2]); //doi tu char thanh long int
t = clock();
for (i=0; i < ((MEGABYTE/BUFFERSIZE)*megabytes); i++) //ghi buffer vao fo theo megabytes lan
write(fo,buffer,BUFFERSIZE); //ghi tu buffer vao disk
t = clock() - t;
double write_time = (((double)t) / CLOCKS_PER_SEC); //tinh thoi gian ghi theo s
double rate = megabytes/write_time;
if (write_time > 0)
printf("Write time: %f s\nWrite rate: %.2f MB/s\n", write_time, rate); //m
close(fo);
return 0;
}
Mục đích của chương trình là để đo tốc độ ghi vào đĩa, mình đặt clock() như vậy đã đúng chưa nhỉ?
Mình chạy chương trình này mất tầm 30s, mà không hiểu sao write_time
khi ra toàn là nhỏ hơn 1s (ví dụ như 0,9123 s).