文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>read buf大小测试

read buf大小测试

时间:2007-06-04  来源:closetome123

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

相关阅读 更多 +
排行榜 更多 +
开局一个小兵最新版

开局一个小兵最新版

休闲益智 下载
火柴人联盟2腾讯qq登录版

火柴人联盟2腾讯qq登录版

体育竞技 下载
tsuki odyssey游戏(月兔冒险奥德赛)

tsuki odyssey游戏(月兔冒险奥德赛)

休闲益智 下载