0

I am using PHP and SimpleXML and was wondering which will be faster and use less memory. I am not concerned with element vs attribute, but more on the perforance with PHP. There are 10 departments and 20 different categories. A file can belong to multiple departments and categories.

<file dep1="1" dep2="1" dep3="0" cat1="0" cat2="0" cat3="1" path="\path\to\file">
    file description
</file>

or

<file path="\path\to\file">
    <departments dep1="1" dep2="1" dep3="0" />
    <categories cat1="0" cat2="0" cat3="1" />
    <description>file description</description>
</file>
2
  • 1
    Did you try to benchmark the performance with each? Commented Dec 14, 2011 at 15:57
  • I did some benchmarks (not an expert at benchmarking). Created two xml files with 1000 <file> elements each. Iterated 100 times (100000 iterations). On my machine, the first method (having dep and cats as attributes to the file element took on average 0.0169 seconds and the second method took 0.0417 seconds. Even though the second method takes slightly longer, it better describes the data. Need to find a middleground... Commented Dec 14, 2011 at 19:44

3 Answers 3

1

Using attributes (or elements) named dep1, dep2, dep3, ... for the first N departments is not good XML design. This has nothing to do with machine performance, it's all about making the data usable, the code that processes it easy to write, and the structure extensible.

Repeating data should be in repeating elements with the same name.

Sign up to request clarification or add additional context in comments.

Comments

0

Performance will be pretty much equal. Attribute nodes and element nodes in a DOM are basically the same data structure, they are derived from the same base class, the will perform the same.

The point is more: What XML structure matches your data. That's the one you should use.

I'd tend to use something like this, this feels most natural to me:

<file>
    <path>\path\to\file</path>
    <description>file description</description>
    <departments>
        <dep id="1" />
        <dep id="2" />
        <!-- the ones that are 0 are omitted -->
    </departments>
    <categories>
        <cat id="3" />
    </categories>
</file>

Comments

0

If i understand your question right:

Unless your final XML is several megabytes heavy, this shouldnt pose a problem.

Your performance problem will lie in the way you analyse and work with the data more than how big is your XML file.

1 Comment

The file wont be more than 1MB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.