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. Problem when convert 50MB string values to Stream...

Problem when convert 50MB string values to Stream...

Scheduled Pinned Locked Moved C#
helptutorialquestion
19 Posts 7 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.
  • L Lost User

    Did you know you can use strings in a switch?

    E Offline
    E Offline
    Ennis Ray Lynch Jr
    wrote on last edited by
    #10

    Poor form, I always use String.Compare instead of a switch. I also believe strings are over and misused in modern programming.

    Need custom software developed? I do C# development and consulting all over the United States. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

    L 1 Reply Last reply
    0
    • E Ennis Ray Lynch Jr

      Poor form, I always use String.Compare instead of a switch. I also believe strings are over and misused in modern programming.

      Need custom software developed? I do C# development and consulting all over the United States. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #11

      Ennis Ray Lynch, Jr. wrote:

      Poor form

      Opinion :) I don't like huge if/else if/else if/else if/else if/else if blocks (and then there is the O(n) time complexity of it..)

      E A 2 Replies Last reply
      0
      • L Lost User

        Ennis Ray Lynch, Jr. wrote:

        Poor form

        Opinion :) I don't like huge if/else if/else if/else if/else if/else if blocks (and then there is the O(n) time complexity of it..)

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #12

        I believe the complexity of both is O(1) and you may be confusing a run time estimate with complexity. Yes it is my opinion, but then again I like not having to know about the culture when doing string comparisons, http://msdn.microsoft.com/en-us/library/hyxc48dt.aspx[^] But you are right, it is just my opinion and both will run fine, and in .NET, the switch will be faster.

        Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

        L 1 Reply Last reply
        0
        • E Ennis Ray Lynch Jr

          I believe the complexity of both is O(1) and you may be confusing a run time estimate with complexity. Yes it is my opinion, but then again I like not having to know about the culture when doing string comparisons, http://msdn.microsoft.com/en-us/library/hyxc48dt.aspx[^] But you are right, it is just my opinion and both will run fine, and in .NET, the switch will be faster.

          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #13

          Actually they are both O(k) (*), strange eh? They thought it would be a good idea to make a dictionary that maps all the strings in the cases to integers, and then switch on that. And that dictionary, is a local variable! Argh! So it's remade every time, so that'll be k Dictionary.Add's every time.. great.. The if/else if chain is trivially O(k), there are k cases (definition of k) and if no match is found at all then all cases were tried so k string comparisons (which aren't O(1) by themselves, unless both strings are interned). Both ways make no sense. 1) There is a total order over strings, so a tree-based search could be used. 2) That "temporary" dictionary should not be temporary, so the cost of adding the entries can be amortized. Anyway, it's true that it's O(1) if you look at the resulting exe/dll, but you can change the number of cases in the source so it's really a "variable" after all

          E 1 Reply Last reply
          0
          • L Lost User

            Actually they are both O(k) (*), strange eh? They thought it would be a good idea to make a dictionary that maps all the strings in the cases to integers, and then switch on that. And that dictionary, is a local variable! Argh! So it's remade every time, so that'll be k Dictionary.Add's every time.. great.. The if/else if chain is trivially O(k), there are k cases (definition of k) and if no match is found at all then all cases were tried so k string comparisons (which aren't O(1) by themselves, unless both strings are interned). Both ways make no sense. 1) There is a total order over strings, so a tree-based search could be used. 2) That "temporary" dictionary should not be temporary, so the cost of adding the entries can be amortized. Anyway, it's true that it's O(1) if you look at the resulting exe/dll, but you can change the number of cases in the source so it's really a "variable" after all

            E Offline
            E Offline
            Ennis Ray Lynch Jr
            wrote on last edited by
            #14

            Removing the loop removes the n, which does not seem logical except that Big Oh is not a measure of complexity and not running time.

            for(int i=0;i
            is O(n)
            but

            SomeAction(0);
            SomeAction(1);
            ...
            SomeAction(n-1);
            

            is O(1)

            Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting.

            A man said to the universe:
            "Sir I exist!"
            "However," replied the universe,
            "The fact has not created in me
            A sense of obligation."
            --Stephen Crane

            L 2 Replies Last reply
            0
            • L Lost User

              Ennis Ray Lynch, Jr. wrote:

              Poor form

              Opinion :) I don't like huge if/else if/else if/else if/else if/else if blocks (and then there is the O(n) time complexity of it..)

              A Offline
              A Offline
              Alaric_
              wrote on last edited by
              #15

              Ennis Ray Lynch, Jr. wrote:

              I also believe strings are over and misused in modern programming.

              Agreed

              harold aptroot wrote:

              Opinion Smile I don't like huge if/else if/else if/else if/else if/else if blocks (and then there is the O(n) time complexity of it..)

              Agreed [This is a pet peeve of mine as well] Simple solution to work a string value into a switch statement without working with the strings directly - Create an enumeration that KEYS your valid values (therefore, your Enum keys would have to be an exact representation of the string you expect) and simply

              switch(stringValue)
              {
              case enumerator.Value1.ToString(): //Will switch on string value "Value1"
              break;
              case enumerator.Value2.ToString(); //Will switch on string value "Value2"
              break;
              }

              This is not even close to the most robust solution along these lines, but it I highly prefer it to switch cases containing arbitrary string values or endless if/else if/else if/else statements :-D I actually have a more robust solution which involves creating(among other things):

              public static T Parse(string value)
              {
              if (value.Equals(String.Empty)){
              Parse("NonDeterminant"); //"NonDeterminant" is an application-defined invalid state because 'INVALID' usually is an actionable state
              }
              return (T)Enum.Parse(typeof(T), value);
              }

              within my translation layer that will allow you to specify which type of Enumerator you are attempting to cast the string to and return out the type-safe Enumerated value of the string - essentially maps your string to a list of acceptable values and disregards that it ever existed as a string

              "I need build Skynet. Plz send code"

              1 Reply Last reply
              0
              • E Ennis Ray Lynch Jr

                Removing the loop removes the n, which does not seem logical except that Big Oh is not a measure of complexity and not running time.

                for(int i=0;i
                is O(n)
                but

                SomeAction(0);
                SomeAction(1);
                ...
                SomeAction(n-1);
                

                is O(1)

                Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting.

                A man said to the universe:
                "Sir I exist!"
                "However," replied the universe,
                "The fact has not created in me
                A sense of obligation."
                --Stephen Crane

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #16

                I really have to disagree. You have something of non-constant length n, so you can't simplify the n away. It's really just a special case of unrolling And this n doesn't suddenly become constant just because you said "ok so let's not change it anymore" - that's like saying sorting an array is constant time for any given array

                E 1 Reply Last reply
                0
                • L Lost User

                  I really have to disagree. You have something of non-constant length n, so you can't simplify the n away. It's really just a special case of unrolling And this n doesn't suddenly become constant just because you said "ok so let's not change it anymore" - that's like saying sorting an array is constant time for any given array

                  E Offline
                  E Offline
                  Ennis Ray Lynch Jr
                  wrote on last edited by
                  #17

                  In analyzing the complexity of algorithms you need to examine the code as written, not the compiler output. While I try to be forgiving in the matter of opinion this is a matter of fact. I am not going to get pedantic on the matter and I am not going to try and prove why this is the case. You don't have to believe me but I would suggest doing a lot more research in the matter before deciding to disagree further.

                  Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

                  L 1 Reply Last reply
                  0
                  • E Ennis Ray Lynch Jr

                    In analyzing the complexity of algorithms you need to examine the code as written, not the compiler output. While I try to be forgiving in the matter of opinion this is a matter of fact. I am not going to get pedantic on the matter and I am not going to try and prove why this is the case. You don't have to believe me but I would suggest doing a lot more research in the matter before deciding to disagree further.

                    Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #18

                    Come one, be flexible, the number of cases is the real variable here, if you start treating it as a constant (and YES I am very well aware that it actually is a constant) it becomes impossible to say anything interesting about it anymore. So I say O(k), and k is a constant as usual (it's a k, not an n or m or whatever), but if you do that last simplification to O(1) you just lose the necessary detail without gaining anything.

                    1 Reply Last reply
                    0
                    • E Ennis Ray Lynch Jr

                      Removing the loop removes the n, which does not seem logical except that Big Oh is not a measure of complexity and not running time.

                      for(int i=0;i
                      is O(n)
                      but

                      SomeAction(0);
                      SomeAction(1);
                      ...
                      SomeAction(n-1);
                      

                      is O(1)

                      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting.

                      A man said to the universe:
                      "Sir I exist!"
                      "However," replied the universe,
                      "The fact has not created in me
                      A sense of obligation."
                      --Stephen Crane

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #19

                      And not if you write it like this. You'd have to fix the n. The "..." is not of fixed length now. edit: yea ok sorry that was just useless. There is a difference between what is right and what is useful. Actually that applies to this whole argument so if you don't mind I'll just quit.


                      Last modified: 31mins after originally posted --

                      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