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

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

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

Qt - QDomDocument

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#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 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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
posted @   南水之源  阅读(1625)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2015-12-17 [ios][swift]swift 怎么去除 optional
点击右上角即可分享
微信分享提示