Download Speed
-
Im trying to make an app, that i can download my mails from my account.
For the test i have 4 mails, total 200kb. When i download the mails, it takes 30 secs.. aprox.. A LOT! and i dont know how to improve the velocity.
Thanks!
The code that i use to download is this:
ns = networkstream from a TCP, buffer 8192.
private string getemailtest2(int indexservidor)
{
StringBuilder resp1 = new StringBuilder();
bool begin = true;int compa = 8193; System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); while (true) { byte\[\] dataaleer = new byte\[8192\]; if (ns.DataAvailable || begin) { int a = ns.Read(dataaleer, 0, 8192); } resp1.Append(enc.GetString(dataaleer).Trim('\\0')); if (begin) { compa = Int32.Parse(resp1.ToString().Split(' ')\[1\]); begin = false; } int leng = enc.GetBytes(resp1.ToString()).Length; if (leng >= compa) { if (!ns.DataAvailable) { break; } } } return resp1.ToString();
-
Im trying to make an app, that i can download my mails from my account.
For the test i have 4 mails, total 200kb. When i download the mails, it takes 30 secs.. aprox.. A LOT! and i dont know how to improve the velocity.
Thanks!
The code that i use to download is this:
ns = networkstream from a TCP, buffer 8192.
private string getemailtest2(int indexservidor)
{
StringBuilder resp1 = new StringBuilder();
bool begin = true;int compa = 8193; System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); while (true) { byte\[\] dataaleer = new byte\[8192\]; if (ns.DataAvailable || begin) { int a = ns.Read(dataaleer, 0, 8192); } resp1.Append(enc.GetString(dataaleer).Trim('\\0')); if (begin) { compa = Int32.Parse(resp1.ToString().Split(' ')\[1\]); begin = false; } int leng = enc.GetBytes(resp1.ToString()).Length; if (leng >= compa) { if (!ns.DataAvailable) { break; } } } return resp1.ToString();
Who is responsible for that mess? Have you checked the CPU activity, say with Task Manager? if it is high and you don't get real bandwidth, it indicates you are wasting lots of CPU cycles. Here are some pointers: 1. Every iteration of the while loop, you create a new buffer (even when no data available); you read data into it, then trim the entire buffer, i.e. you scan all of it to find out it is empty or only partially filled, whereas Read did return the exact length. 2. Every iteration of the while loop, you perform an enc.GetBytes() on a string that is always growing; however you already know the previous length, and how much got added. IMO you don't need GetBytes at all. 3. StringBuilder is said to be cheaper than string for a sequence of operations. However, when it needs to grow, it doubles its capacity by allocating a new array and copying all the data; you can create a StringBuilder with a specific initial capacity, reducing/avoiding the need for such copies. Suggestions: 1. with ASCII encoding, what would be the difference between the number of bytes received, and the number of characters in the final text? 2. throw your code away, and start afresh. :)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
Who is responsible for that mess? Have you checked the CPU activity, say with Task Manager? if it is high and you don't get real bandwidth, it indicates you are wasting lots of CPU cycles. Here are some pointers: 1. Every iteration of the while loop, you create a new buffer (even when no data available); you read data into it, then trim the entire buffer, i.e. you scan all of it to find out it is empty or only partially filled, whereas Read did return the exact length. 2. Every iteration of the while loop, you perform an enc.GetBytes() on a string that is always growing; however you already know the previous length, and how much got added. IMO you don't need GetBytes at all. 3. StringBuilder is said to be cheaper than string for a sequence of operations. However, when it needs to grow, it doubles its capacity by allocating a new array and copying all the data; you can create a StringBuilder with a specific initial capacity, reducing/avoiding the need for such copies. Suggestions: 1. with ASCII encoding, what would be the difference between the number of bytes received, and the number of characters in the final text? 2. throw your code away, and start afresh. :)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
Also: If you call ToString on the StringBuilder in every iteration in the loop, that will force it to allocate a new buffer and copy the data to it when you continue to add data to it. You lose any benefits of using the StringBuilder, the performance gets as bad as using += on a string. So you will have shuffled around several gigabytes of data before you are done reading the 200 kilobytes.
Despite everything, the person most likely to be fooling you next is yourself.