Chinaunix首页 | 论坛 | 博客
  • 博客访问: 105832
  • 博文数量: 23
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 235
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-19 22:44
文章分类

全部博文(23)

文章存档

2011年(1)

2008年(1)

2007年(21)

我的朋友
最近访客

分类: LINUX

2007-06-04 14:36:30

dd if=/dev/zero of=./test.file bs=512 count=102400

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include "/dvr/debug/gcc4_debug.h"


int
main(int argc, char **argv)
{

    int fd;
    int cache_size;
    char *buf;
    off_t size_tmp;
    off_t size;
    struct stat fd_info;
    struct timeval tpstart;
    struct timeval tpend;

    if(argc != 3)
        ERRX(Argv error ! \n $bin file size_cache);


    if((fd = open(argv[1], O_RDONLY)) < 0)
        ERRX(open error);

    if(-1 == lstat(argv[1], &fd_info))
        ERRX(lstat error);

    if((cache_size = atoi(argv[2])) < 0)
        ERRX(cache_size error);

    buf = (char *)malloc(cache_size);
    if(NULL == buf)
        ERRX(malloc error);

    size = 0;
    size_tmp = 0;
    gettimeofday(&tpstart, NULL);
    while(1)
    {
        if(0 == (size_tmp = read(fd, buf, cache_size)))
            break;
        size += size_tmp;
    }
    gettimeofday(&tpend, NULL);
    free(buf);
    close(fd);

// printf("size : %u; size : %u;\n", size, fd_info.st_size);

    if(tpend.tv_usec < tpstart.tv_usec)
    {
        tpend.tv_usec += 1000000;
        tpend.tv_sec -= 1;
    }

    printf("cahce_size:%10d \t time: \t %20ld \n", cache_size,
     tpend.tv_usec - tpstart.tv_usec +
     1000000 * (tpend.tv_sec - tpstart.tv_sec));
    return 0;
}



测试结果4K最为理想:

cahce_size: 1 time: 36961018
cahce_size: 2 time: 18467070
cahce_size: 4 time: 10092231
cahce_size: 8 time: 4772089
cahce_size: 16 time: 2400306
cahce_size: 32 time: 1203176
cahce_size: 64 time: 615936
cahce_size: 128 time: 317510
cahce_size: 256 time: 185227
cahce_size: 512 time: 103166
cahce_size: 1024 time: 65666
cahce_size: 2048 time: 42172
cahce_size: 4096 time: 28747
cahce_size: 8192 time: 31170
cahce_size: 16484 time: 26659
cahce_size: 32768 time: 25488
cahce_size: 65536 time: 25222
cahce_size: 131072 time: 29782

阅读(2408) | 评论(0) | 转发(0) |
0

上一篇:RGB显示-python-tk

下一篇:timer linux编程

给主人留下些什么吧!~~