需求: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;