Writing to MemoryStream with foreach

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












As you can see here:



enter image description here



20% CPU in a 3 minutes and 20 seconds excessive 40 executions async/await at a time for 9700 times caused quite a bit of CPU usage.



Code:



public MemoryStream getMessageBody()

#region Get MessageBody
IEnumerable<BytesWraper> MessageBody = GetMessageBodySource();
if (MessageBody == null)
return null;

#endregion
#region Write MessageBody's Data to a MemoryStream so we can convert it later to a String
MemoryStream memoryStream = new MemoryStream(ContentLength == -1 ? 0 : ContentLength);
try
foreach (BytesWraper bytes in MessageBody)
memoryStream.Write(bytes.Value, 0, bytes.Length);

catch (Exception)
return null;

#endregion
if (ConnectionClosed())
_request.Dispose();

MessageBodyLoaded = true;
return memoryStream;



Is there a way here to reduce the amount of usage by this foreach call? Perhaps not need to do it at all?



All im then doing with the result is as follows:



return MessageBody != null ? CharacterSet.GetString(MessageBody.GetBuffer(), 0, (int)MessageBody.Length) : null


(where MessageBody is getMessageBody())







share|improve this question

















  • 3




    Could you explain what BytesWrapper is and what you are actually doing here? Saying this code is slow, need help isn't very clear. Help us help you and explain what you're doing in more detail.
    – t3chb0t
    May 3 at 9:56










  • I can't work out how to parse "20% CPU in a 3 minutes and 20 seconds excessive 40 executions async/await at a time for 9700 times caused quite a bit of CPU usage." I understand that the total execution time of your test is 200 seconds and that this loop is responsible for 20% of this and potentially a bottleneck, but I can't see how to fit the middle of the line into the sentence.
    – Peter Taylor
    May 3 at 10:06










  • BytesWrapper simply a get; set of .Value (byte) and .Length (int of byte.length) @t3chb0t
    – Ma Dude
    May 3 at 10:18










  • Basically its grabbing bytes from a TCP response and then I simply convert it to a string.
    – Ma Dude
    May 3 at 10:18
















up vote
0
down vote

favorite












As you can see here:



enter image description here



20% CPU in a 3 minutes and 20 seconds excessive 40 executions async/await at a time for 9700 times caused quite a bit of CPU usage.



Code:



public MemoryStream getMessageBody()

#region Get MessageBody
IEnumerable<BytesWraper> MessageBody = GetMessageBodySource();
if (MessageBody == null)
return null;

#endregion
#region Write MessageBody's Data to a MemoryStream so we can convert it later to a String
MemoryStream memoryStream = new MemoryStream(ContentLength == -1 ? 0 : ContentLength);
try
foreach (BytesWraper bytes in MessageBody)
memoryStream.Write(bytes.Value, 0, bytes.Length);

catch (Exception)
return null;

#endregion
if (ConnectionClosed())
_request.Dispose();

MessageBodyLoaded = true;
return memoryStream;



Is there a way here to reduce the amount of usage by this foreach call? Perhaps not need to do it at all?



All im then doing with the result is as follows:



return MessageBody != null ? CharacterSet.GetString(MessageBody.GetBuffer(), 0, (int)MessageBody.Length) : null


(where MessageBody is getMessageBody())







share|improve this question

















  • 3




    Could you explain what BytesWrapper is and what you are actually doing here? Saying this code is slow, need help isn't very clear. Help us help you and explain what you're doing in more detail.
    – t3chb0t
    May 3 at 9:56










  • I can't work out how to parse "20% CPU in a 3 minutes and 20 seconds excessive 40 executions async/await at a time for 9700 times caused quite a bit of CPU usage." I understand that the total execution time of your test is 200 seconds and that this loop is responsible for 20% of this and potentially a bottleneck, but I can't see how to fit the middle of the line into the sentence.
    – Peter Taylor
    May 3 at 10:06










  • BytesWrapper simply a get; set of .Value (byte) and .Length (int of byte.length) @t3chb0t
    – Ma Dude
    May 3 at 10:18










  • Basically its grabbing bytes from a TCP response and then I simply convert it to a string.
    – Ma Dude
    May 3 at 10:18












up vote
0
down vote

favorite









up vote
0
down vote

favorite











As you can see here:



enter image description here



20% CPU in a 3 minutes and 20 seconds excessive 40 executions async/await at a time for 9700 times caused quite a bit of CPU usage.



Code:



public MemoryStream getMessageBody()

#region Get MessageBody
IEnumerable<BytesWraper> MessageBody = GetMessageBodySource();
if (MessageBody == null)
return null;

#endregion
#region Write MessageBody's Data to a MemoryStream so we can convert it later to a String
MemoryStream memoryStream = new MemoryStream(ContentLength == -1 ? 0 : ContentLength);
try
foreach (BytesWraper bytes in MessageBody)
memoryStream.Write(bytes.Value, 0, bytes.Length);

catch (Exception)
return null;

#endregion
if (ConnectionClosed())
_request.Dispose();

MessageBodyLoaded = true;
return memoryStream;



Is there a way here to reduce the amount of usage by this foreach call? Perhaps not need to do it at all?



All im then doing with the result is as follows:



return MessageBody != null ? CharacterSet.GetString(MessageBody.GetBuffer(), 0, (int)MessageBody.Length) : null


(where MessageBody is getMessageBody())







share|improve this question













As you can see here:



enter image description here



20% CPU in a 3 minutes and 20 seconds excessive 40 executions async/await at a time for 9700 times caused quite a bit of CPU usage.



Code:



public MemoryStream getMessageBody()

#region Get MessageBody
IEnumerable<BytesWraper> MessageBody = GetMessageBodySource();
if (MessageBody == null)
return null;

#endregion
#region Write MessageBody's Data to a MemoryStream so we can convert it later to a String
MemoryStream memoryStream = new MemoryStream(ContentLength == -1 ? 0 : ContentLength);
try
foreach (BytesWraper bytes in MessageBody)
memoryStream.Write(bytes.Value, 0, bytes.Length);

catch (Exception)
return null;

#endregion
if (ConnectionClosed())
_request.Dispose();

MessageBodyLoaded = true;
return memoryStream;



Is there a way here to reduce the amount of usage by this foreach call? Perhaps not need to do it at all?



All im then doing with the result is as follows:



return MessageBody != null ? CharacterSet.GetString(MessageBody.GetBuffer(), 0, (int)MessageBody.Length) : null


(where MessageBody is getMessageBody())









share|improve this question












share|improve this question




share|improve this question








edited May 3 at 15:35









t3chb0t

31.9k54195




31.9k54195









asked May 3 at 9:17









Ma Dude

32




32







  • 3




    Could you explain what BytesWrapper is and what you are actually doing here? Saying this code is slow, need help isn't very clear. Help us help you and explain what you're doing in more detail.
    – t3chb0t
    May 3 at 9:56










  • I can't work out how to parse "20% CPU in a 3 minutes and 20 seconds excessive 40 executions async/await at a time for 9700 times caused quite a bit of CPU usage." I understand that the total execution time of your test is 200 seconds and that this loop is responsible for 20% of this and potentially a bottleneck, but I can't see how to fit the middle of the line into the sentence.
    – Peter Taylor
    May 3 at 10:06










  • BytesWrapper simply a get; set of .Value (byte) and .Length (int of byte.length) @t3chb0t
    – Ma Dude
    May 3 at 10:18










  • Basically its grabbing bytes from a TCP response and then I simply convert it to a string.
    – Ma Dude
    May 3 at 10:18












  • 3




    Could you explain what BytesWrapper is and what you are actually doing here? Saying this code is slow, need help isn't very clear. Help us help you and explain what you're doing in more detail.
    – t3chb0t
    May 3 at 9:56










  • I can't work out how to parse "20% CPU in a 3 minutes and 20 seconds excessive 40 executions async/await at a time for 9700 times caused quite a bit of CPU usage." I understand that the total execution time of your test is 200 seconds and that this loop is responsible for 20% of this and potentially a bottleneck, but I can't see how to fit the middle of the line into the sentence.
    – Peter Taylor
    May 3 at 10:06










  • BytesWrapper simply a get; set of .Value (byte) and .Length (int of byte.length) @t3chb0t
    – Ma Dude
    May 3 at 10:18










  • Basically its grabbing bytes from a TCP response and then I simply convert it to a string.
    – Ma Dude
    May 3 at 10:18







3




3




Could you explain what BytesWrapper is and what you are actually doing here? Saying this code is slow, need help isn't very clear. Help us help you and explain what you're doing in more detail.
– t3chb0t
May 3 at 9:56




Could you explain what BytesWrapper is and what you are actually doing here? Saying this code is slow, need help isn't very clear. Help us help you and explain what you're doing in more detail.
– t3chb0t
May 3 at 9:56












I can't work out how to parse "20% CPU in a 3 minutes and 20 seconds excessive 40 executions async/await at a time for 9700 times caused quite a bit of CPU usage." I understand that the total execution time of your test is 200 seconds and that this loop is responsible for 20% of this and potentially a bottleneck, but I can't see how to fit the middle of the line into the sentence.
– Peter Taylor
May 3 at 10:06




I can't work out how to parse "20% CPU in a 3 minutes and 20 seconds excessive 40 executions async/await at a time for 9700 times caused quite a bit of CPU usage." I understand that the total execution time of your test is 200 seconds and that this loop is responsible for 20% of this and potentially a bottleneck, but I can't see how to fit the middle of the line into the sentence.
– Peter Taylor
May 3 at 10:06












BytesWrapper simply a get; set of .Value (byte) and .Length (int of byte.length) @t3chb0t
– Ma Dude
May 3 at 10:18




BytesWrapper simply a get; set of .Value (byte) and .Length (int of byte.length) @t3chb0t
– Ma Dude
May 3 at 10:18












Basically its grabbing bytes from a TCP response and then I simply convert it to a string.
– Ma Dude
May 3 at 10:18




Basically its grabbing bytes from a TCP response and then I simply convert it to a string.
– Ma Dude
May 3 at 10:18










1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Have you tried something like this (?):



MemoryStream memoryStream = new MemoryStream(MessageBody.SelectMany(mb => mb.Value).ToArray());





share|improve this answer

















  • 1




    Edit: So I actually went with your one as it actually essentially did the same as mine, and if there was any performance impact then it wouldnt have been much like at all. Just edit it to the following: Encoding.UTF8.GetString(MessageBody.SelectMany(mb => mb.Value).ToArray()); as this returns as a string which is essentially what I was wanting.
    – Ma Dude
    May 3 at 12:38










  • @ImPRAGMA mhmm... but the foreach should be faster here anyway so it's not necessarily an improvement if your goal is better performance. Did you measure it?
    – t3chb0t
    May 3 at 12:54










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

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: "196"
;
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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193543%2fwriting-to-memorystream-with-foreach%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Have you tried something like this (?):



MemoryStream memoryStream = new MemoryStream(MessageBody.SelectMany(mb => mb.Value).ToArray());





share|improve this answer

















  • 1




    Edit: So I actually went with your one as it actually essentially did the same as mine, and if there was any performance impact then it wouldnt have been much like at all. Just edit it to the following: Encoding.UTF8.GetString(MessageBody.SelectMany(mb => mb.Value).ToArray()); as this returns as a string which is essentially what I was wanting.
    – Ma Dude
    May 3 at 12:38










  • @ImPRAGMA mhmm... but the foreach should be faster here anyway so it's not necessarily an improvement if your goal is better performance. Did you measure it?
    – t3chb0t
    May 3 at 12:54














up vote
0
down vote



accepted










Have you tried something like this (?):



MemoryStream memoryStream = new MemoryStream(MessageBody.SelectMany(mb => mb.Value).ToArray());





share|improve this answer

















  • 1




    Edit: So I actually went with your one as it actually essentially did the same as mine, and if there was any performance impact then it wouldnt have been much like at all. Just edit it to the following: Encoding.UTF8.GetString(MessageBody.SelectMany(mb => mb.Value).ToArray()); as this returns as a string which is essentially what I was wanting.
    – Ma Dude
    May 3 at 12:38










  • @ImPRAGMA mhmm... but the foreach should be faster here anyway so it's not necessarily an improvement if your goal is better performance. Did you measure it?
    – t3chb0t
    May 3 at 12:54












up vote
0
down vote



accepted







up vote
0
down vote



accepted






Have you tried something like this (?):



MemoryStream memoryStream = new MemoryStream(MessageBody.SelectMany(mb => mb.Value).ToArray());





share|improve this answer













Have you tried something like this (?):



MemoryStream memoryStream = new MemoryStream(MessageBody.SelectMany(mb => mb.Value).ToArray());






share|improve this answer













share|improve this answer



share|improve this answer











answered May 3 at 12:25









Henrik Hansen

3,8381417




3,8381417







  • 1




    Edit: So I actually went with your one as it actually essentially did the same as mine, and if there was any performance impact then it wouldnt have been much like at all. Just edit it to the following: Encoding.UTF8.GetString(MessageBody.SelectMany(mb => mb.Value).ToArray()); as this returns as a string which is essentially what I was wanting.
    – Ma Dude
    May 3 at 12:38










  • @ImPRAGMA mhmm... but the foreach should be faster here anyway so it's not necessarily an improvement if your goal is better performance. Did you measure it?
    – t3chb0t
    May 3 at 12:54












  • 1




    Edit: So I actually went with your one as it actually essentially did the same as mine, and if there was any performance impact then it wouldnt have been much like at all. Just edit it to the following: Encoding.UTF8.GetString(MessageBody.SelectMany(mb => mb.Value).ToArray()); as this returns as a string which is essentially what I was wanting.
    – Ma Dude
    May 3 at 12:38










  • @ImPRAGMA mhmm... but the foreach should be faster here anyway so it's not necessarily an improvement if your goal is better performance. Did you measure it?
    – t3chb0t
    May 3 at 12:54







1




1




Edit: So I actually went with your one as it actually essentially did the same as mine, and if there was any performance impact then it wouldnt have been much like at all. Just edit it to the following: Encoding.UTF8.GetString(MessageBody.SelectMany(mb => mb.Value).ToArray()); as this returns as a string which is essentially what I was wanting.
– Ma Dude
May 3 at 12:38




Edit: So I actually went with your one as it actually essentially did the same as mine, and if there was any performance impact then it wouldnt have been much like at all. Just edit it to the following: Encoding.UTF8.GetString(MessageBody.SelectMany(mb => mb.Value).ToArray()); as this returns as a string which is essentially what I was wanting.
– Ma Dude
May 3 at 12:38












@ImPRAGMA mhmm... but the foreach should be faster here anyway so it's not necessarily an improvement if your goal is better performance. Did you measure it?
– t3chb0t
May 3 at 12:54




@ImPRAGMA mhmm... but the foreach should be faster here anyway so it's not necessarily an improvement if your goal is better performance. Did you measure it?
– t3chb0t
May 3 at 12:54












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193543%2fwriting-to-memorystream-with-foreach%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods