Reputation: 1249
I'm seeking some design advice relating to avoiding a potential performance issue.
Short version: Generally speaking, how much slower is querying a DOM using XPath than querying two hashmaps?
Longer and mpre specific version:
I'm creating a Java application which has to read an XML file as part of its start-up. The file will probably have a maximum of 8200 attributes configured as shown in the below XML. Theoretically there would be a maximum 100 mapping Elements each with 2 attributes. Each element containing 1 to 40 elements with pairs of attributes. So a total of (40 * 2) * 100 + 200 text attributes.
<mapping source="A" target="B">
<field fname="" tname=""/>
<field fname="" tname=""/>
<field fname="" tname=""/>
<field fname="" tname=""/>
</mapping>
It crossed my mind to extract the info into 2 hashmaps which will then be used to do lookups during the execution of the program. I'm only interested in finding the mapping "source" from which I will lookup both the corresponding "target" and then use any child "fname" to look up the corresponding "tname".
I'm just wondering how much slower it would be to use XPath and query the DOM each time I want some information compared with creating a hashmap of (e.g.) a "mappings class" which itself contains a hashmap of fname/tname kv pairs...
Thanks in advance for any tips and hope this makes sense.
Kevin
Upvotes: 0
Views: 260
Reputation: 12817
You will of course hide the implementation, whatever it will be, behind a proper interface?
interface Target {
String target();
String tname(String fname);
}
interface Mapping {
Target get(String source);
}
Apart from that, I'm pretty sure HashMaps will outperform XPath in this case, but since your interface doesn't reveal the implementation, you are free to change it.
Upvotes: 1
Reputation: 16060
Premature Optimization is the root of (almost) all evil.
Is your code slow?
-> No: Leave it as it is
-> Yes: Measure it and the fix the bottleneck (if found).
But from my experience: The XPath is implemented in most cases onto of a DOM. This is some kind of memory overhead.
I believe the HashMap will be faster and consume less memory.
Upvotes: 1