Cool. Thanks.
Out of curiosity, I tried it with a file made of zeroes:
./speedtest test.img test1.img
data written: 200.00 MB
time taken: 0.30 seconds
average rate:656.45 MB/s
And a file with random data:
./speedtest testu.img testu1.img
data written: 200.00 MB
time taken: 0.58 seconds
average rate:345.70 MB/s
I think these rates are still kind of optimistic (but I don't know C well enough to be critical of the process), but my point is still the type of data being written has an impact on performance.
Thanks.
I think this is the best test you can have since all it does is copy a file from memory to disk and hence the speed you will get is a reflection only of the capacity of the hard drive to take it data.The test is equivalent to download 200MB of data into memory and then commit all of it to disk with a single command.
The code is pretty simple,this is how it works
int fd1 = open( file1,O_RDONLY ) ; //<----- 1
int fd2 = open( file2,O_RDONLY ) ; //<-------2
void * map = mmap( 0,st.st_size,PROT_READ,MAP_PRIVATE,fd1,0 ) ;//<-------3
clock_gettime( CLOCK_REALTIME,&start );//<------4
write( fd2,map,st.st_size ) ;//<-------5
clock_gettime( CLOCK_REALTIME,&end );//<--------6
The first command opens the source file
The second one opens the destination file
The third commands maps the source file into memory to allow file access as if it was simple memory access.Possible alternative to mapping the file is to read the whole of it into memory first,or read a bit of it or source file into memory and write it to destination file in a tight loop,this is doable would inefficient
The forth command record the current time in the "start" structure
The fifth command write the file to disk
The sixth command record the current time in the "end" structure
The rest of the code calculate the time difference btw the two times,do simple math and report the time.