I am writing a program that is very critical to performance. I am polling an atom feed that usually has about 50 entries. I need to parse through this can get the uri link as fast as possible.
Currently I am doing this:
var feedUrl = "my path";
using (var feedReader = XmlReader.Create(feedUrl))
{
var feedContent = SyndicationFeed.Load(feedReader);
if (null == feedContent) return null;
foreach (var item in feedContent.Items.Reverse())
{
if (item.Title.Text.Contains("Some text I am looking for"))
{
foreach (var link in item.Links)
{
uri = link.Uri;
}
}
}
}
Ive read from many sources that using a for loop is much faster than using foreach so I am trying to implement this but keep getting some errors that say cannot apply indexing to the SyndicationItem. This seems to be because SyndicationItem is an IEnumerable.
So this leaves me with two questions:
1.) Is there a better more efficient/faster way to do this
2.) Am I currently implementing the best solution?
The vast majority of time is likely getting spent in the round-trip to pick up the feed in the first place. Typically, network latency time will dwarf the amount of time it takes to iterate over a collection.
However, it is possible that your
feedContent.Itemsis not fully loaded into memory at once, and consequently iterating over theReverseof this collection may cause a lot more overhead than you need. I’d personally suggest using a LINQ statement to grab the items you want in a single pass, then call.ToList()to put the results in an in-memory collection that is easily reversible before calling.Reverse().Or, if you’re just interested in a single URI (your code makes it look like you’re just trying to end up with the first URI in the first match), you might as well just call
.FirstOrDefault()and skip theReverseandToListentirely:Update
I played with this a little and (as I suspected) roughly 95% of the cost of this program (testing against a feed with 25 items on Blogger) is taken up with the calls to
XmlReader.Create()andSyndicationFeed.Load(). Trying to optimize theforloop is like measuring with a micrometer, marking with a wax pencil, and cutting with an axe.Update 2
In response to your comment: Yes, there is a way, and it’s pretty simple, too.
And just to reiterate, this won’t give you any significant performance improvement, but the code is much more expressive of what you’re really trying to do, and therefore much easier to maintain.