[转]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 |
[转]xml解析工具的效率比较QDomDocument、TinyXml-2、RapidXml、PugiXml的更多相关文章
- Java XML解析工具 dom4j介绍及使用实例
Java XML解析工具 dom4j介绍及使用实例 dom4j介绍 dom4j的项目地址:http://sourceforge.net/projects/dom4j/?source=directory ...
- NSXMLParser自定义的一个xml解析工具
// // DenglXMLParser.h // #import <Foundation/Foundation.h> @interface DenglXMLParser : NSXMLP ...
- xml解析工具-jdom
前言:近期接触SSH框架的时候,经常得配置一下xml文件:今天闲来没事就挖挖xml解析的原理供大伙儿分享.本文主要通过一个简单的例子解析一个xml文件.明白其中缘由之后,大家想定义自己的xml也绝非难 ...
- XML解析工具类
public class XmlUtil { /* * 利用dom4j解析xml文件内容,并返回map数据形式 * path是.xml文件所在的路径 */ public static Map<S ...
- xml解析工具mashaller javaee自带解析类
1.怎样去掉Marshaller的格式化? : JAXBContext context = JAXBContext.newInstance(Entity.class); Marshaller mars ...
- Java -- XML解析工具dom4j
前言 XML现已成为一种通用的数据交流方式,它的平台无关性.语言无关性.系统无关性.给数据集成与交互带来了极大的方便,对于XML的解析有四种方式:DOM生成和解析XML文档,SAX生成和解析XML文件 ...
- java自带的xml解析工具类
public class JaxbUtil { /** * java对象转换为xml文件 * * @param xmlPath xml文件路径 * @param load java对象.Class * ...
- Android 简易XML解析
首先创建在Android工程中创建一个Assets文件夹 app/src/main/assets 在这里添加一个名为 data.xml的文件,然后编辑这个文件,加入如下XML格式内容 <?xml ...
- UI进阶 解析XML 解析JSON
1.数据解析 解析的基本概念 所谓“解析”:从事先规定好的格式中提取数据 解析的前提:提前约定好格式,数据提供方按照格式提供数据.数据获取方则按照格式获取数据 iOS开发常见的解析:XML解析.JSO ...
随机推荐
- 基于STM32F4移植W5500官方驱动库ioLibrary_Driver(转)
源: 基于STM32F4移植W5500官方驱动库ioLibrary_Driver 参考: 基于STM32+W5500 的Ethernet和Internet移植 Upgrade W5500 Throug ...
- Caused by: java.lang.ClassNotFoundException: Illegal access: this web application instance has been stopped already. Could not load [org.jboss.netty.util.internal.ByteBufferUtil]. The following stack
Caused by: java.lang.ClassNotFoundException: Illegal access: this web application instance has been ...
- Spring框架之使用JdbcTemplate开发Dao层程序
简介: JdbcTemplate开发dao层程序 由Spring框架给我们提供,Spring提供的很多操作数据源(关系型数据库,二维表格模型,有明确的行和列(mysql/orcal等) 非关系 ...
- JDK源码之ThreadLocal
1.定义 ThreadLocal是线程变量,就是说每一个线程都有对应的该变量副本,线程修改该变量时,线程与线程之间的变量是相互隔离的,互相并看不见.这个结构附带在线程上,一个线程可以根据ThreadL ...
- linux apidoc的安装和使用
1.先去官网下载已编译好的安装包 以Centos7.4 64位为例, 下载地址: https://nodejs.org/dist/v8.1.2/node-v8.1.2-linux-x64.tar.xz ...
- hihocoder [Offer收割]编程练习赛14
A.小Hi和小Ho的礼物 谜之第1题,明明是第1题AC率比C还要低.题目是求在n个不同重量袋子选4袋,2袋给A,2袋给B,使2人获得重量相同,求问方案数. 我也是一脸懵b...o(n2)暴力枚举发现把 ...
- win10常见问题处理办法
1.当笔记本连接wifi时,提示,无internet,安全,而手机能正常连接wifi时: cmd(需管理员权限)执行命令 netsh winsock reset 出现已重置,重启电脑 解决方法 2.当 ...
- How to Conduct High-Impact Research and Produce High-Quality Papers
How to Conduct High-Impact Research and Produce High-Quality Papers Contents From Slide Russell Grou ...
- ssh-copy-id命令解析
ssh-copy-id命令可以把本地主机的公钥复制到远程主机的authorized_keys文件上, ssh-copy-id命令也会给远程主机的用户主目录(home)和~/.ssh, 和~/.ssh/ ...
- How to install Maven on Windows
To install Apache Maven on Windows, you just need to download the Maven’s zip file, and Unzip it to ...