c# - Linq performance parsing XML file -


i need parse xml file (1~10 mb); using xdocument purpose.

at moment using linq query xml document, need improve performance of application , thought replace linq query old style loops, did not have boost.

following piece of code called query:

stopwatch stopwatch = new stopwatch(); xdocument xdoc = xdocument.load("filename.xml"); string def = "version"; xelement xelm;  stopwatch.start();  (int = 0; < 1000; i++)     xelm = xdoc.descendants("def").where(d => d.attribute("name").value == def).single();  stopwatch.stop(); console.writeline(stopwatch.elapsedmilliseconds);   stopwatch.restart();  (int = 0; < 1000; i++) {     foreach (var elm in xdoc.descendants("def"))     {         if (elm.attribute("name").value == def)         {             xelm = elm;             break;         }     } }  stopwatch.stop(); console.writeline(stopwatch.elapsedmilliseconds); 

the elapsed time pretty same both versions, , me result quite strange since thought where method of linq has create new list when invoked.

why both versions have same performace? there way improve original code?

linq uses deferred (lazy) execution, means iterates through collections once , when needs to.

deferred execution can improve performance when have manipulate large data collections, in programs contain series of chained queries or manipulations. in best case, deferred execution enables single iteration through source collection.

in case, there no new list being generated where clause. in fact, compiled version of linq statement function in same way foreach statement.

edit: idea improving performance follow robert mckee's answer, says should use .first() instead of .single() not have iterate through entire list. beyond that, i'm not sure else can do, except use different library or different language.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -