📄 blastin.c
字号:
/* blastin.c */
/* Reads a file and report the I/O results */
/* By Bob Russell */
/* vi: set autoindent tabstop=4 shiftwidth=4 : */
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <fcntl.h>
#define MAX_BYTES (1024 * 1024)
#define READ_MAX (1024 * 1024)
char buffer[MAX_BYTES];
int main( int argc, char *argv[] )
{
int fd, secs, usecs, bytes, n, nbytes, left, k;
struct timeval start_time, stop_time;
double dtime, dbytes;
char *ptr;
if( argc != 2 && argc != 3 )
{
printf("Usage: blastin file-name [Megabytes]\n");
exit(EXIT_FAILURE);
}
if( (fd = open(argv[1],O_RDONLY)) < 0 )
{
printf("open %s: %s\n", argv[1], strerror(errno));
exit(EXIT_FAILURE);
}
if( argc == 3 )
{
errno = 0;
nbytes = strtoul(argv[2], &ptr, 0);
if( errno != 0 || ptr == argv[2] || *ptr != '\0' )
{
printf("Illegal number: %s\n", argv[2]);
exit(EXIT_FAILURE);
}
nbytes *= MAX_BYTES;
}
else
nbytes = INT_MAX;
printf("Reading %10d bytes total\n", nbytes);
if( gettimeofday(&start_time, NULL) < 0 )
{
printf("gettimeofday start: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
n = bytes = 0;
/* printf("%10d\n", bytes); */
k = left = nbytes;
if( k > READ_MAX )
k = READ_MAX;
while( left > 0 && (n = read(fd, buffer, k)) > 0 )
{
bytes += n;
/* printf("%10d\n", bytes); */
left -= n;
k = left;
if( k > READ_MAX )
k = READ_MAX;
}
/* printf("%10d\n", bytes); */
if( gettimeofday(&stop_time, NULL) < 0 )
{
printf("gettimeofday stop: %s\n", strerror(errno));
}
if( n < 0 )
{
printf("read: %s\n", strerror(errno));
}
if( start_time.tv_usec > stop_time.tv_usec )
{
stop_time.tv_usec += 1000000;
stop_time.tv_sec -= 1;
}
secs = stop_time.tv_sec - start_time.tv_sec;
usecs = stop_time.tv_usec - start_time.tv_usec;
printf("%18d bytes read\n", bytes);
printf("%11d.%06d seconds\n", secs, usecs);
dtime = secs;
dtime *= 1000000.0;
dtime += usecs;
dbytes = bytes;
if( dtime != 0 )
{
printf("%18.3f Megabytes/second\n", dbytes/dtime);
printf("%18.3f Megabits/second\n\n", (8.0*dbytes)/dtime);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -