Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Download Speed

Download Speed

Scheduled Pinned Locked Moved C#
performancetutorialcode-review
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gonzalodiaz
    wrote on last edited by
    #1

    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();
    
    L 1 Reply Last reply
    0
    • G gonzalodiaz

      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();
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.


      G 1 Reply Last reply
      0
      • L Luc Pattyn

        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.


        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups