XmlReader Async VS Sinc












0















I know that when calling asynchronous methods a Thread is allocated from the pool and this has a cost.



XmlReader has asynchronous implementations:



using (var r = XmlReader.Create(fs, new XmlReaderSettings() { Async = true }))
{
while (await r.ReadAsync())
{
if (r.IsStartElement())
{
switch (r.Name)
{
case "a": var a = await r.ReadElementContentAsStringAsync(); break;
case "b": var b = await r.ReadElementContentAsStringAsync(); break;
case "c": var c = await r.ReadElementContentAsStringAsync(); break;
}
}
}
}


Wouldn't it be a waste of processing to allocate threads in the pool to perform a process as simple as reading an xml element?



I imagine that asynchronous reads in xml should be used only when the content of the tag is known to be large, such as strings in base64, etc.



The code above written synchronously, wouldn't be more performance? Of course, reading about 5,000 xml files or a single large xml file.










share|improve this question


















  • 3





    I know that when calling asynchronous methods a Thread is allocated from the pool and this has a cost. where did you get this information ?

    – Michael Randall
    Jan 19 at 23:48






  • 2





    It depends. If you're just reading one file then indeed you won't gain anything from using the asynchronous version. If you're reading 100 files in parallel then you should definitely go for async. When it comes to performance there is really only one rule: measure, then make your decision

    – Kevin Gosse
    Jan 19 at 23:48











  • I voted for closing as this will vary from one use case to another, as people said measure what you want to do.

    – Ivan Ičin
    Jan 19 at 23:56






  • 1





    Your question might be based on incorrect assumptions, take a look at What is the difference between asynchronous programming and multithreading? and Async/Await vs Threads. But see also Async I/O intensive code is running slower than non-async, why? and Can using async-await give you any performance benefits?.

    – dbc
    Jan 20 at 0:08











  • Asynchronous method has advantage in three cases 1) You have to access data quickly before it is overridden. An example where a UART has a small buffer like 8 bytes and you must read the data before it is lost 2) When you are trying to run items in parallel to get improved performance. 3) Where blocking in synchronous mode will prevent other methods from running. Like in a form project where blocking may lock up the form.

    – jdweng
    Jan 20 at 11:17
















0















I know that when calling asynchronous methods a Thread is allocated from the pool and this has a cost.



XmlReader has asynchronous implementations:



using (var r = XmlReader.Create(fs, new XmlReaderSettings() { Async = true }))
{
while (await r.ReadAsync())
{
if (r.IsStartElement())
{
switch (r.Name)
{
case "a": var a = await r.ReadElementContentAsStringAsync(); break;
case "b": var b = await r.ReadElementContentAsStringAsync(); break;
case "c": var c = await r.ReadElementContentAsStringAsync(); break;
}
}
}
}


Wouldn't it be a waste of processing to allocate threads in the pool to perform a process as simple as reading an xml element?



I imagine that asynchronous reads in xml should be used only when the content of the tag is known to be large, such as strings in base64, etc.



The code above written synchronously, wouldn't be more performance? Of course, reading about 5,000 xml files or a single large xml file.










share|improve this question


















  • 3





    I know that when calling asynchronous methods a Thread is allocated from the pool and this has a cost. where did you get this information ?

    – Michael Randall
    Jan 19 at 23:48






  • 2





    It depends. If you're just reading one file then indeed you won't gain anything from using the asynchronous version. If you're reading 100 files in parallel then you should definitely go for async. When it comes to performance there is really only one rule: measure, then make your decision

    – Kevin Gosse
    Jan 19 at 23:48











  • I voted for closing as this will vary from one use case to another, as people said measure what you want to do.

    – Ivan Ičin
    Jan 19 at 23:56






  • 1





    Your question might be based on incorrect assumptions, take a look at What is the difference between asynchronous programming and multithreading? and Async/Await vs Threads. But see also Async I/O intensive code is running slower than non-async, why? and Can using async-await give you any performance benefits?.

    – dbc
    Jan 20 at 0:08











  • Asynchronous method has advantage in three cases 1) You have to access data quickly before it is overridden. An example where a UART has a small buffer like 8 bytes and you must read the data before it is lost 2) When you are trying to run items in parallel to get improved performance. 3) Where blocking in synchronous mode will prevent other methods from running. Like in a form project where blocking may lock up the form.

    – jdweng
    Jan 20 at 11:17














0












0








0








I know that when calling asynchronous methods a Thread is allocated from the pool and this has a cost.



XmlReader has asynchronous implementations:



using (var r = XmlReader.Create(fs, new XmlReaderSettings() { Async = true }))
{
while (await r.ReadAsync())
{
if (r.IsStartElement())
{
switch (r.Name)
{
case "a": var a = await r.ReadElementContentAsStringAsync(); break;
case "b": var b = await r.ReadElementContentAsStringAsync(); break;
case "c": var c = await r.ReadElementContentAsStringAsync(); break;
}
}
}
}


Wouldn't it be a waste of processing to allocate threads in the pool to perform a process as simple as reading an xml element?



I imagine that asynchronous reads in xml should be used only when the content of the tag is known to be large, such as strings in base64, etc.



The code above written synchronously, wouldn't be more performance? Of course, reading about 5,000 xml files or a single large xml file.










share|improve this question














I know that when calling asynchronous methods a Thread is allocated from the pool and this has a cost.



XmlReader has asynchronous implementations:



using (var r = XmlReader.Create(fs, new XmlReaderSettings() { Async = true }))
{
while (await r.ReadAsync())
{
if (r.IsStartElement())
{
switch (r.Name)
{
case "a": var a = await r.ReadElementContentAsStringAsync(); break;
case "b": var b = await r.ReadElementContentAsStringAsync(); break;
case "c": var c = await r.ReadElementContentAsStringAsync(); break;
}
}
}
}


Wouldn't it be a waste of processing to allocate threads in the pool to perform a process as simple as reading an xml element?



I imagine that asynchronous reads in xml should be used only when the content of the tag is known to be large, such as strings in base64, etc.



The code above written synchronously, wouldn't be more performance? Of course, reading about 5,000 xml files or a single large xml file.







c# xml performance asynchronous xmlreader






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 19 at 23:34









Vinicius GonçalvesVinicius Gonçalves

9601132




9601132








  • 3





    I know that when calling asynchronous methods a Thread is allocated from the pool and this has a cost. where did you get this information ?

    – Michael Randall
    Jan 19 at 23:48






  • 2





    It depends. If you're just reading one file then indeed you won't gain anything from using the asynchronous version. If you're reading 100 files in parallel then you should definitely go for async. When it comes to performance there is really only one rule: measure, then make your decision

    – Kevin Gosse
    Jan 19 at 23:48











  • I voted for closing as this will vary from one use case to another, as people said measure what you want to do.

    – Ivan Ičin
    Jan 19 at 23:56






  • 1





    Your question might be based on incorrect assumptions, take a look at What is the difference between asynchronous programming and multithreading? and Async/Await vs Threads. But see also Async I/O intensive code is running slower than non-async, why? and Can using async-await give you any performance benefits?.

    – dbc
    Jan 20 at 0:08











  • Asynchronous method has advantage in three cases 1) You have to access data quickly before it is overridden. An example where a UART has a small buffer like 8 bytes and you must read the data before it is lost 2) When you are trying to run items in parallel to get improved performance. 3) Where blocking in synchronous mode will prevent other methods from running. Like in a form project where blocking may lock up the form.

    – jdweng
    Jan 20 at 11:17














  • 3





    I know that when calling asynchronous methods a Thread is allocated from the pool and this has a cost. where did you get this information ?

    – Michael Randall
    Jan 19 at 23:48






  • 2





    It depends. If you're just reading one file then indeed you won't gain anything from using the asynchronous version. If you're reading 100 files in parallel then you should definitely go for async. When it comes to performance there is really only one rule: measure, then make your decision

    – Kevin Gosse
    Jan 19 at 23:48











  • I voted for closing as this will vary from one use case to another, as people said measure what you want to do.

    – Ivan Ičin
    Jan 19 at 23:56






  • 1





    Your question might be based on incorrect assumptions, take a look at What is the difference between asynchronous programming and multithreading? and Async/Await vs Threads. But see also Async I/O intensive code is running slower than non-async, why? and Can using async-await give you any performance benefits?.

    – dbc
    Jan 20 at 0:08











  • Asynchronous method has advantage in three cases 1) You have to access data quickly before it is overridden. An example where a UART has a small buffer like 8 bytes and you must read the data before it is lost 2) When you are trying to run items in parallel to get improved performance. 3) Where blocking in synchronous mode will prevent other methods from running. Like in a form project where blocking may lock up the form.

    – jdweng
    Jan 20 at 11:17








3




3





I know that when calling asynchronous methods a Thread is allocated from the pool and this has a cost. where did you get this information ?

– Michael Randall
Jan 19 at 23:48





I know that when calling asynchronous methods a Thread is allocated from the pool and this has a cost. where did you get this information ?

– Michael Randall
Jan 19 at 23:48




2




2





It depends. If you're just reading one file then indeed you won't gain anything from using the asynchronous version. If you're reading 100 files in parallel then you should definitely go for async. When it comes to performance there is really only one rule: measure, then make your decision

– Kevin Gosse
Jan 19 at 23:48





It depends. If you're just reading one file then indeed you won't gain anything from using the asynchronous version. If you're reading 100 files in parallel then you should definitely go for async. When it comes to performance there is really only one rule: measure, then make your decision

– Kevin Gosse
Jan 19 at 23:48













I voted for closing as this will vary from one use case to another, as people said measure what you want to do.

– Ivan Ičin
Jan 19 at 23:56





I voted for closing as this will vary from one use case to another, as people said measure what you want to do.

– Ivan Ičin
Jan 19 at 23:56




1




1





Your question might be based on incorrect assumptions, take a look at What is the difference between asynchronous programming and multithreading? and Async/Await vs Threads. But see also Async I/O intensive code is running slower than non-async, why? and Can using async-await give you any performance benefits?.

– dbc
Jan 20 at 0:08





Your question might be based on incorrect assumptions, take a look at What is the difference between asynchronous programming and multithreading? and Async/Await vs Threads. But see also Async I/O intensive code is running slower than non-async, why? and Can using async-await give you any performance benefits?.

– dbc
Jan 20 at 0:08













Asynchronous method has advantage in three cases 1) You have to access data quickly before it is overridden. An example where a UART has a small buffer like 8 bytes and you must read the data before it is lost 2) When you are trying to run items in parallel to get improved performance. 3) Where blocking in synchronous mode will prevent other methods from running. Like in a form project where blocking may lock up the form.

– jdweng
Jan 20 at 11:17





Asynchronous method has advantage in three cases 1) You have to access data quickly before it is overridden. An example where a UART has a small buffer like 8 bytes and you must read the data before it is lost 2) When you are trying to run items in parallel to get improved performance. 3) Where blocking in synchronous mode will prevent other methods from running. Like in a form project where blocking may lock up the form.

– jdweng
Jan 20 at 11:17












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272282%2fxmlreader-async-vs-sinc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272282%2fxmlreader-async-vs-sinc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Callistus III

Plistias Cous

Index Sanctorum