SimpleXML vs XMLWriter vs DOM

I have done a little Performance test for the 3 different methods available in PHP for XML generation. For this test I wrote a little script which generates the same XML File using the different methods.

 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
<?php

function printMemory()
{
    echo "Used " . (memory_get_peak_usage() / 1024 / 1024) . " MB\n";
}

function simpleXMLTest($nodes, $subnodes)
{
    $xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><resultset/>");
    for ($i=0; $i<$nodes; $i++) {
        $node = $xml->addChild('feed' . $i);
        for ($j=0; $j<$subnodes; $j++) {
            $subnode = $node->addChild('feed' . $j);
            $subnode->addAttribute('feed', $j);
        }
    }
    printMemory();
    file_put_contents('/tmp/_test.xml', $xml->asXML());
}

function XMLWriterTest($nodes, $subnodes)
{
    $xml = new XMLWriter();
    $xml->openMemory();
    $xml->startDocument('1.0', 'UTF-8');
    $xml->startElement('resultset');
    for ($i=0; $i<$nodes; $i++) {
        $xml->startElement('test' . $i);
        for ($j=0; $j<$subnodes; $j++) {
            $xml->startElement('test' . $j);
            $xml->writeAttribute('test', $j);
            $xml->endElement();
        }
        $xml->endElement();
    }
    $xml->endElement();
    printMemory();
    file_put_contents('/tmp/_test.xml', $xml->outputMemory(true));
}

function DOMTest($nodes, $subnodes)
{
    $dom = new DOMDocument('1.0', 'utf-8');
    $resultset = $dom->createElement('resultset');
    for ($i=0; $i<$nodes; $i++) {
        $node = $dom->createElement('test' . $i);
        for ($j=0; $j<$subnodes; $j++) {
            $subnode = $dom->createElement('test' . $j);
            $subnode->setAttribute('test', $j);
            $node->appendChild($subnode);
        }
        $resultset->appendChild($node);
    }
    $dom->appendChild($resultset);
    printMemory();
    file_put_contents('/tmp/_test.xml', $dom->saveXML());
}

$nodes = 1000;
$subnodes = 100;

$start = microtime(true);

//simpleXMLTest($nodes, $subnodes);
//XMLWriterTest($nodes, $subnodes);
//DOMTest($nodes, $subnodes);

$end = microtime(true) - $start;
echo "Took " . $end . " ms\n";
  1. Test: 1000 Nodes with 100 Subnodes for each Node. (XML Filesize: 1.9MB)

      SimpleXML XMLWriter DOM
    Time 0.48202991485596 ms 0.36775803565979 ms 0.72811722755432 ms
    Memory 0.6109619140625 MB 0.61136627197266 MB 0.6141357421875 MB

    XML Comparison - Nodes 1000 - Subnodes 100

  2. Test: 1000 Nodes with 1000 Subnodes for each Node. (XML Filesize: 20MB)

      SimpleXML XMLWriter DOM
    Time 4.867399930954 ms 3.5637412071228 ms 7.1697089672089 ms
    Memory 0.6109619140625 MB 0.61136627197266 MB 0.6141357421875 MB

    XML Comparison - Nodes 1000 - Subnodes 1000

  3. Test: 10000 Nodes with 1000 Subnodes for each Node. (XML Filesize: 199MB)

      SimpleXML XMLWriter DOM
    Time 129.83511686325 ms 38.820327997208 ms 365.01518082619 ms
    Memory 0.6109619140625 MB 0.61136627197266 MB 0.6141357421875 MB

    XML Comparison - Nodes 10000 - Subnodes 1000

  • Memory Usage: There isn’t a big difference between the three methods in Memory Usage (~0.002 MB)

    XML Comparison - Memory Usage

After this Tests, we can see that DOM is not an optimal method for XML generation. DOM with small files needs +0.40 seconds more then XMLWriter, and SimpleXML needs only +0.08 seconds more.

With bigger files, XMLWriter is much faster than the other two methods. So if you need to create big XML files, the best solution is XMLWriter


Eclipse/ZendStudio Custom Username for Code Templates
Git Over HTTP (Git-Http-Backend)
comments powered by Disqus