[转]xml解析工具的效率比较QDomDocument、TinyXml-2、RapidXml、PugiXml

转自:http://www.itdaan.com/blog/2017/02/20/301ad47832f4.html

由于windows环境下测试不稳定,博主选择在linux下进行的测试!

Qt - QDomDocument

#include <QtCore/QCoreApplication>
#include <qdom.h>
#include <QFile>
#include <QIODevice>
#include <iostream>
#ifdef Q_OS_WIN
# include <Windows.h>
#else
# include <sys/time.h>
#endif using std::cout;
using std::endl; #define TEST_TIMES 10 int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); #ifdef Q_OS_WIN //< windows long tStart = 0;
long tEnd = 0; LARGE_INTEGER nFreq;
LARGE_INTEGER nStartTime;
LARGE_INTEGER nEndTime;
double time = 0.; QueryPerformanceFrequency(&nFreq);
QFile file( "D:/DriverConfig.xml" );
QDomDocument doc; for( int i = 0; i < TEST_TIMES; ++i )
{
doc.clear(); //< step1 open file
if( !file.open(QIODevice::ReadOnly) )
{
cout << "failed to open file!" << endl;
continue;
}
Sleep( 100 );
QueryPerformanceCounter(&nStartTime); //< step2 set content
if( !doc.setContent(&file) )
{
cout << "Failed to read xml file!" << endl;
}
QueryPerformanceCounter(&nEndTime);
time = (double)(nEndTime.QuadPart-nStartTime.QuadPart) / (double)nFreq.QuadPart * 1000.; //< ms
cout << " seting content costs " << time << "ms" << endl; file.close();
Sleep( 100 );
} #else //< LINUX timeval starttime, endtime;
QFile file( "/home/liuyc/DriverConfig.xml" );
QDomDocument doc;
double timeuse = 0.;
double timeAverage = 0.; for( int i = 0; i < TEST_TIMES; ++i )
{
doc.clear(); //< step1 open file
if( !file.open(QIODevice::ReadOnly) )
{
cout << "failed to open file!" << endl;
continue;
}
sleep( 1 ); //< delay for 1s
gettimeofday( &starttime, 0 ); //< step2 set content
if( !doc.setContent(&file) )
{
cout << "Failed to read xml file!" << endl;
continue;
}
gettimeofday( &endtime, 0 );
timeuse = 1000000. * (endtime.tv_sec - starttime.tv_sec) + endtime.tv_usec - starttime.tv_usec;
timeuse *= 0.001 ;
timeAverage += timeuse;
cout << " reading files costs : " << timeuse << "ms" << endl; file.close();
sleep( 1 ); //< delay for 1s
} timeAverage /= TEST_TIMES;
cout << " The End *****************\n average costs = " << timeAverage << "ms" << endl; #endif return a.exec();
}

  

TinyXml-2

#include <iostream>
#include "tinyxml2.h"
#ifdef _WIN32
#include <Windows.h>
#else
#include <sys/time.h>
#endif
using namespace tinyxml2;
using std::cout;
using std::endl; #define TEST_TIMES 10 int main()
{
#ifndef _WIN32 //< linux ------------------------------------------------ tinyxml2::XMLDocument doc;
timeval starttime, endtime;
double timeuse = 0.;
double timeAverage = 0.;
for( int i = 0; i < TEST_TIMES; ++i )
{
gettimeofday( &starttime, 0 );
if( XML_SUCCESS != doc.LoadFile( "/home/liuyc/DriverConfig.xml" ) )
{
cout << "failed in load xml file! _ " << i << endl;
continue;
}
gettimeofday( &endtime, 0 ); timeuse = 1000000. * (endtime.tv_sec - starttime.tv_sec) + endtime.tv_usec - starttime.tv_usec;
timeuse *= 0.001 ;
cout << " reading files costs : " << timeuse << "ms" << endl;
timeAverage += timeuse;
}
timeAverage /= TEST_TIMES;
cout << " \n** The end *******************\n the average costs = " << timeAverage << "ms" << endl; #else //< windows --------------------------------------------------- LARGE_INTEGER nFreq;
LARGE_INTEGER nStartTime;
LARGE_INTEGER nEndTime;
double time = 0.; QueryPerformanceFrequency(&nFreq);
tinyxml2::XMLDocument doc;
for( int i = 0; i < TEST_TIMES; ++i )
{
QueryPerformanceCounter(&nStartTime);
if( XML_SUCCESS != doc.LoadFile( "D:/DriverConfig.xml" ) )
{
cout << "failed in load xml file! _ " << i << endl;
continue;
}
QueryPerformanceCounter(&nEndTime);
time = (double)(nEndTime.QuadPart-nStartTime.QuadPart) / (double)nFreq.QuadPart * 1000.; //< ms
cout << " reading files costs : " << time << "ms" << endl;
}
cout << endl;
system("pause"); #endif //< end of windows ---------------------------------------------------
return 0;
}

  

RapidXml

RapidXml版本: 1.13

#include <iostream>
#include "rapidxml.hpp"
#include "rapidxml_print.hpp"
#include "rapidxml_utils.hpp"
#ifdef _WIN32
# include <Windows.h>
#else
# include <sys/time.h>
#endif using namespace rapidxml;
using std::cout;
using std::endl; #define TEST_TIMES 10 int main()
{
#ifdef _WIN32 //< windows LARGE_INTEGER nFreq;
LARGE_INTEGER nStartTime;
LARGE_INTEGER nEndTime;
double time = 0.;
QueryPerformanceFrequency(&nFreq); //< parse xml
for( int i = 0 ; i < TEST_TIMES; ++i )
{
rapidxml::file<> filename( "D:/DriverConfig.xml" );
xml_document<> doc;
QueryPerformanceCounter(&nStartTime); doc.parse<0>( filename.data() ); QueryPerformanceCounter(&nEndTime);
time = (double)(nEndTime.QuadPart-nStartTime.QuadPart) / (double)nFreq.QuadPart * 1000.; //< ms
cout << " reading files costs : " << time << "ms" << endl;
doc.clear();
} system("pause"); #else timeval starttime, endtime;
double timeuse = 0.;
double timeAverage = 0.; //< parse xml
for( int i = 0 ; i < TEST_TIMES; ++i )
{
rapidxml::file<> filename( "/home/liuyc/DriverConfig.xml" );
xml_document<> doc;
gettimeofday( &starttime, 0 ); doc.parse<0>( filename.data() ); gettimeofday( &endtime, 0 ); timeuse = 1000000. * (endtime.tv_sec - starttime.tv_sec) + endtime.tv_usec - starttime.tv_usec;
timeuse *= 0.001 ;
cout << " reading files costs : " << timeuse << "ms" << endl;
doc.clear(); timeAverage += timeuse;
}
timeAverage /= TEST_TIMES;
cout << " \n** The end *******************\n the average costs = " << timeAverage << "ms" << endl; #endif return 0;
}

  

PugiXml

#include <iostream>
#include "pugixml.hpp"
#include "pugiconfig.hpp"
#include <sys/time.h>
using namespace std; #define TEST_TIMES 10 int main( void )
{
pugi::xml_document doc;
timeval starttime, endtime;
double timeuse = 0.;
double timeAverage = 0.;
for( int i = 0; i < TEST_TIMES; ++i )
{
gettimeofday( &starttime, 0 );
if( !doc.load_file( "/home/liuyc/DriverConfig.xml" ) )
{
cout << "failed in load xml file! _ " << i << endl;
continue;
}
gettimeofday( &endtime, 0 ); timeuse = 1000000. * (endtime.tv_sec - starttime.tv_sec) + endtime.tv_usec - starttime.tv_usec;
timeuse *= 0.001 ;
cout << " reading files costs : " << timeuse << "ms" << endl;
timeAverage += timeuse;
}
timeAverage /= TEST_TIMES;
cout << " \n** The end *******************\n the average costs = " << timeAverage << "ms" << endl;
return 0;
}

  

总结

统计的时间如下(LINUX):

解析器 消耗时间(ms) 效率倍数(相对Qt)
Qt-QDomDocument 25.85 1
TinyXml2 6.64 3.89
RapidXml 2.71 9.54
pugixml 1.57 16.47
上一篇:js判断浏览器是否安装Flash插件,并提示安装或开启


下一篇:【译】第二篇 Integration Services:SSIS数据泵