473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

whats the fastest way to lookup nodes in a list

I have a very "flat" doc structure like this

<root>
<one>
<two>
<three>
...
n <=100
</root>

And i was wondering what is the fastest method for finding the Nth sub
element of root with a specific node name.
Or the quickest way to find the node whose name might be "SixtyFive"

Would it be:

For(int i =0;i< list.Count;++i) // or foreach allowing for the poss overhead
of a call to GetEnumerator
{
if(list[i].localname == thingiwant)
{
// do stuff
}
}

or

root.GetElementsByTagName // presumably uses SelectNodes?

or

root.SelectSingleNode // presumably some overhead here setting up the
"query engine". will have to use string.parse/concat to build the query

I'm about to profile some of this, but was wondering what anyone with a bit
more expereince of this stuff would think.

TIA

bg


Nov 12 '05 #1
2 1382
microsoft wrote:
I have a very "flat" doc structure like this

<root>
<one>
<two>
<three>
...
n <=100
</root>

And i was wondering what is the fastest method for finding the Nth sub
element of root with a specific node name.
Or the quickest way to find the node whose name might be "SixtyFive"


If the number of elements is really small, all methods should work fast
enough. I'd go with trivial GetElementsByTagName or SelectSingleNode. If
you really want to optimize it, you can have a preallocated lookup table
to convert SixtyFive to 65 and then get node by index, e.g. using
doc.SelectSingleNode("/root/*[65]"). If your queries are repetitives
ones, you can index nodes by name using IndexingXPathNavigator and then
select nodes in constant running time, a matter of nanoseconds.

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com
Nov 12 '05 #2
Given a node as the starting point in the tree, apply your favorite match
function while traversing the descendants using FirstChild, NextSibling
and ParentNode a la:

void TraverseDescendants(XmlNode node) {
XmlNode stop, child, sibling, parent;
if (node.HasChildNodes) {
stop = node;
for (;;) {
if ((child = node.FirstChild) != null) {
node = child;
}
else {
for (;;) {
if ((sibling = node.NextSibling) != null) {
node = sibling;
break;
}
else {
if ((parent = node.ParentNode) != stop) {
node = parent;
}
else {
return;
}
}
}
}
}
}
}

Note that for a flat document you can significantly simplify the above
traversal. If you're planning to match names you might want to atomize
them first into the XmlDocument.NameTable so that you can take advantage
of the pointer comparison vs. string comparison.

All other approaches listed below eventually leverage FirstChild,
NextSibling and ParentNode. Do use XmlNode.SelectNodes() over
XmlDocument.GetElementsByTagName() since the laterkeeps track of
changes in the node list.

Ion

"microsoft" <bg@bg.com> wrote in message
news:OH**************@TK2MSFTNGP09.phx.gbl...
I have a very "flat" doc structure like this

<root>
<one>
<two>
<three>
...
n <=100
</root>

And i was wondering what is the fastest method for finding the Nth sub
element of root with a specific node name.
Or the quickest way to find the node whose name might be "SixtyFive"

Would it be:

For(int i =0;i< list.Count;++i) // or foreach allowing for the poss overhead of a call to GetEnumerator
{
if(list[i].localname == thingiwant)
{
// do stuff
}
}

or

root.GetElementsByTagName // presumably uses SelectNodes?

or

root.SelectSingleNode // presumably some overhead here setting up the
"query engine". will have to use string.parse/concat to build the query

I'm about to profile some of this, but was wondering what anyone with a bit more expereince of this stuff would think.

TIA

bg

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Neal D. Becker | last post by:
I need a fairly small lookup table, and I'm wondering which data python data structure would be fastest. I could use a list, tuple, dictionary, numeric array, or maybe plain python array. The...
15
by: Simon Harvey | last post by:
Hi everyone, I am fairly new to learning about xsl and xml, but one thing I have noticed is that anyone offering a tutorial or lesson on it seems to think that its the most incredible invention...
6
by: Jonathan | last post by:
I am hoping that someone more experienced than myself can point me towards what might be the fastest data lookup method to use for storing ip addresses. My situation is that I will need to maintain...
9
by: danny van elsen | last post by:
hello all, I have an application in which I build a list<node>, with potentially thousands of nodes. each node has an "index", and all nodes are ordered by this index. this index reflects a...
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
8
by: rh0dium | last post by:
Hi all, I have a dict which looks like this.. dict={'130nm': {'umc': }, '180nm': {'chartered': , 'tsmc': }, '250nm': {'umc': , 'tsmc': } }
12
by: Godzilla | last post by:
Hello, I'm trying to find a way to convert an integer (8-bits long for starters) and converting them to a list, e.g.: num = 255 numList = with the first element of the list being the...
15
by: caca | last post by:
Hello, This is a question for the best method (in terms of performance only) to choose a random element from a list among those that satisfy a certain property. This is the setting: I need to...
22
by: SETT Programming Contest | last post by:
The SETT Programming Contest: The fastest set<Timplementation Write the fastest set<Timplementation using only standard C++/C. Ideally it should have the same interface like std::set. At least...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.