文件判断存在的效率提升方案(access,stat,fopen)

需求:10w级别的文件打开,需要判断文件是否存在,文件太多影响30%效率

原有写法:
 

                if(_access(path, 00) ==0)
                {

            	    tinyxml2::XMLDocument doc;
                	doc.LoadFile(path);
	                if (doc.Error())
	                {
	                	fclose(ftmp);
	                    	return;
	                }
                    //do soamething
                }

解决方案:

        

​
                if(FIlE*fp =fopen(path, "rb") ==0)
                {

            	    tinyxml2::XMLDocument doc;
                	doc.LoadFile(fp);
	                if (doc.Error())
	                {
	                	fclose(ftmp);
	                    	return;
	                }
                    //do soamething
                }

​

主要比对了:

access,stat,fopen 三个函数的时间,

测试代码如下:(fopen如果不算fclose 时间 非常快快10倍以上)

access,stat,  时间基本相同  这和网上各种版本完全不相同。测试环境(windows) Linux待测试

	time_point<high_resolution_clock> m_start = high_resolution_clock::now();

	GsDir dir1(u8"H:\\10wtif");
	auto files1 = dir1.Files(u8"*.tif");

	int a = 0;
	for (int i = 0; i < files1.size(); i++)
	{
		struct stat sta;
		//a = files1[i].Exists();
		a  = _access(files1[i].FullPath(), 00);
		GsString  fileaux = files1[i].FullPath();
		fileaux += ".aux.xml";
		a = _access(fileaux.c_str(), 00);
		if (i > 5000)
			break;
	}
	double ms = 0.001 * duration_cast<microseconds>(high_resolution_clock::now() - m_start).count();
	cout << "access  cost time: " << ms << " ms" << endl;

	m_start = high_resolution_clock::now();
	for (int i = 0; i < files1.size(); i++)
	{
		struct stat sta;
		if (0 != stat(files1[i].FullPath().c_str(), &sta))
		{
			a = 1;
		}
		GsString  fileaux = files1[i].FullPath();
		fileaux += ".aux.xml";
		a = stat(fileaux.c_str(), &sta);
		if (i > 5000)
			break;
	}
	ms = 0.001 * duration_cast<microseconds>(high_resolution_clock::now() - m_start).count();
	cout << "stat  cost time: " << ms << " ms" << endl;

	FILE* fopenhandle;
	m_start = high_resolution_clock::now();
	for (int i = 0; i < files1.size(); i++)
	{
		struct stat sta;
		if(fopenhandle = fopen(files1[i].FullPath().c_str(), "rb"))
		{
			//fclose(fopenhandle);
		}
		GsString  fileaux = files1[i].FullPath();
		fileaux += ".aux.xml";
		if (fopenhandle = fopen(fileaux.c_str(), "rb"))
		{
			//fclose(fopenhandle);
		}
		if (i > 5000)
			break;
	}
	char dbuffer[2];
	ms = 0.001 * duration_cast<microseconds>(high_resolution_clock::now() - m_start).count();
	cout << "fopen  cost time: " << ms << " ms" << endl;
	if(fopenhandle)
		cout << a << fread(dbuffer,1,1,fopenhandle)<< endl;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值