在VxWorks下,我为了知道读写一个4.5K文件的时间,做了如下两个实验:
实验1:将硬盘上的文件打开,接着读出,然后再写回该文件,最后关闭文件。
#define MAX_LENGTH 4610 int fd; char *flename = "Test.dat"; unsigned char buffer[MAX_LENGTH]; unsigned int readlength, writelength; struct timespec tp1, tp2, tp3, tp4, tp5, tp6; int i;
while ( 1 ) { readlength = 0; writelength = 0; bzero( buffer, MAX_LENGTH ); clock_gettime( CLOCK_REALTIME, &tp1 );
fd = open( flename, O_RDWR, 0 ); if ( ERROR == fd ) { printf( "Don't Open File!\n" ); return; } clock_gettime( CLOCK_REALTIME, &tp2 );
readlength = read( fd, buffer, MAX_LENGTH ); clock_gettime( CLOCK_REALTIME, &tp3 );
for ( i = 0; i < MAX_LENGTH; i++ ) { buffer = ~buffer; }
clock_gettime( CLOCK_REALTIME, &tp4 ); writelength = write( fd, buffer, MAX_LENGTH ); clock_gettime( CLOCK_REALTIME, &tp5 );
close( fd ); clock_gettime( CLOCK_REALTIME, &tp6 );
结果:第一次:OpenTime = 173000000(ns); ReadTime = 17000000(ns); WriteTime = 0(ns); CloseTime = 2000000(ns);
以后的每次:OpenTime = 0(ns); ReadTime = 0(ns); WriteTime = 0(ns); CloseTime = 1000000(ns);
实验2:将硬盘上的文件打开,接着读出,最后关闭文件。
#define MAX_LENGTH 4610 int fd; char *flename = "Test.dat"; unsigned char buffer[MAX_LENGTH]; unsigned int readlength, writelength; struct timespec tp1, tp2, tp3, tp4, tp5, tp6; int i;
while ( 1 ) { readlength = 0; writelength = 0; bzero( buffer, MAX_LENGTH ); clock_gettime( CLOCK_REALTIME, &tp1 );
fd = open( flename, O_RDWR, 0 ); if ( ERROR == fd ) { printf( "Don't Open File!\n" ); return; } clock_gettime( CLOCK_REALTIME, &tp2 );
readlength = read( fd, buffer, MAX_LENGTH ); clock_gettime( CLOCK_REALTIME, &tp3 );
close( fd ); clock_gettime( CLOCK_REALTIME, &tp6 );
结果:第一次:OpenTime = 185000000(ns); ReadTime = 17000000(ns); CloseTime = 0(ns);
以后的每次:OpenTime = 2000000(ns); ReadTime = 5000000(ns); CloseTime = 0(ns);
疑问:
1、为什么在加入了write后,相反的所用的整个时间竟然变短了???
2、为什么在加入了write后,Open、Read、Write都不需要时间,而Close却需要很长的时间?相反的,在只有Read的代码中,Open、Read是需要时间开销的,但Close却不需要时间呢?
[align=right][color=#000066][此贴子已经被作者于2005-1-13 11:39:26编辑过][/color][/align]