SD卡速度测试

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <time.h>
#include <sys/time.h>

#define MAX_FILE_NAME_LEN       (1024)
#define SIZE_M                  (200)

int usDiff(struct  timeval* start, struct  timeval* end)
{
	int diff = 1000000L * (end->tv_sec - start->tv_sec) + end->tv_usec - start->tv_usec;
	return diff;
}

int main(int argc, const char *argv[])
{
    unsigned int* p_size            = NULL;
	int size                        = 1024*1024*SIZE_M;
	char fname[MAX_FILE_NAME_LEN]   = { 0 };

	int i                           = 0;
	int fd                          = 0;
	struct  timeval    atv0;
	struct  timeval    atv1;
	int diff                        = 0;

    memset(fname, 0, sizeof(fname));
    sprintf(fname, "test.dat");
	
	p_size=(unsigned int*)malloc(size);
	if(p_size == NULL)
	{
		printf("malloc error.\n");
		return -1;
	}
    
	for(i=0; i<size/4; i++)
	{
		p_size[i]=i;
//        if(p_size[i] %1000 == 0)
//            getchar();
//        printf("%d ", p_size[i]);
	}
    
	fd = open (fname, O_WRONLY | O_CREAT | O_APPEND | O_SYNC, 0660);

	gettimeofday(&atv0, NULL);
	if(fd > 0)
	{
		if(write(fd, p_size, size) < 0)
            return -1;
		fsync(fd);
		close(fd);
	}

	gettimeofday(&atv1, NULL);

	diff = usDiff(&atv0, &atv1);

	printf("%d us\n", diff/1000);
	double sss = diff/(double)1000/1000;
	printf("SD card write speed:  %.2f MB~s. \n", (double)size/sss);

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值