Writing to MemoryStream with foreach

Clash 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:

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())
c# stream
add a comment |Â
up vote
0
down vote
favorite
As you can see 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())
c# stream
3
Could you explain whatBytesWrapperis 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
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
As you can see 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())
c# stream
As you can see 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())
c# stream
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 whatBytesWrapperis 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
add a comment |Â
3
Could you explain whatBytesWrapperis 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
add a comment |Â
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());
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 theforeachshould 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
add a comment |Â
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());
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 theforeachshould 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
add a comment |Â
up vote
0
down vote
accepted
Have you tried something like this (?):
MemoryStream memoryStream = new MemoryStream(MessageBody.SelectMany(mb => mb.Value).ToArray());
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 theforeachshould 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
add a comment |Â
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());
Have you tried something like this (?):
MemoryStream memoryStream = new MemoryStream(MessageBody.SelectMany(mb => mb.Value).ToArray());
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 theforeachshould 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
add a comment |Â
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 theforeachshould 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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
Could you explain what
BytesWrapperis 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