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. To string or not to string

To string or not to string

Scheduled Pinned Locked Moved C#
6 Posts 4 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.
  • S Offline
    S Offline
    Skymir
    wrote on last edited by
    #1

    I'm trying to optimize a bit of coding and trying to decide if there's a better way to retrieve a list of paired values. Right now I'm just adding the values to a string and using split to pull them out after the list is made. The question is, would it be faster to use a list, array or some other collection rather than a string for a randomly sized list of 0 to 24 elements. Each element is a pair of single digit numbers. Normally I wouldn't worry about something so trivial, however this piece of code will end up looping a few million times.

    The true man wants two things: danger and play. For that reason he wants woman, as the most dangerous plaything.

    A L 2 Replies Last reply
    0
    • S Skymir

      I'm trying to optimize a bit of coding and trying to decide if there's a better way to retrieve a list of paired values. Right now I'm just adding the values to a string and using split to pull them out after the list is made. The question is, would it be faster to use a list, array or some other collection rather than a string for a randomly sized list of 0 to 24 elements. Each element is a pair of single digit numbers. Normally I wouldn't worry about something so trivial, however this piece of code will end up looping a few million times.

      The true man wants two things: danger and play. For that reason he wants woman, as the most dangerous plaything.

      A Offline
      A Offline
      agent00zelda
      wrote on last edited by
      #2

      I came across this article a few weeks ago, take a look and hopefully it will help... http://msdn.microsoft.com/en-us/library/ms173196.aspx[^]

      G S 2 Replies Last reply
      0
      • A agent00zelda

        I came across this article a few weeks ago, take a look and hopefully it will help... http://msdn.microsoft.com/en-us/library/ms173196.aspx[^]

        G Offline
        G Offline
        Garth J Lancaster
        wrote on last edited by
        #3

        nice find Mia - I'll remember that :-D

        1 Reply Last reply
        0
        • A agent00zelda

          I came across this article a few weeks ago, take a look and hopefully it will help... http://msdn.microsoft.com/en-us/library/ms173196.aspx[^]

          S Offline
          S Offline
          Skymir
          wrote on last edited by
          #4

          Like very wow, great link. Thanks much :) Gonna take a while to digest it all, but I found some great tips I can use now.

          The true man wants two things: danger and play. For that reason he wants woman, as the most dangerous plaything.

          1 Reply Last reply
          0
          • S Skymir

            I'm trying to optimize a bit of coding and trying to decide if there's a better way to retrieve a list of paired values. Right now I'm just adding the values to a string and using split to pull them out after the list is made. The question is, would it be faster to use a list, array or some other collection rather than a string for a randomly sized list of 0 to 24 elements. Each element is a pair of single digit numbers. Normally I wouldn't worry about something so trivial, however this piece of code will end up looping a few million times.

            The true man wants two things: danger and play. For that reason he wants woman, as the most dangerous plaything.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, if the data initially are numbers, not strings, then don't use strings. if the first numbers of all pairs have different values, you have two choices: - the generic Dictionary < int, int > since it stores pairs of numbers, be warned it does not preserve order. - or, since the range is very limited, have a 10-element array and choose a special value to indicate absence of a pair. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


            S 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, if the data initially are numbers, not strings, then don't use strings. if the first numbers of all pairs have different values, you have two choices: - the generic Dictionary < int, int > since it stores pairs of numbers, be warned it does not preserve order. - or, since the range is very limited, have a 10-element array and choose a special value to indicate absence of a pair. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


              S Offline
              S Offline
              Skymir
              wrote on last edited by
              #6

              I thought of those, that's why I was asking if someone had more info on their performance. If I use an array, I have to add in code to check what the last used element was, in order to add new values on to the end. If I use a collection (ordering isn't important for the data) then I have the overhead of a linked list. With the string being essentially an array of char[] I was pretty sure it would come down to either the string or just an array if there was enough overhead removal to warrant the code change. From the link I was given, it looks like that fastest way to go is going to be a 48 element array. Since I'm only dealing with at most 4 of them at a time, they should stay in cache. I only have to keep track of how many elements are there during the adding process to prevent having to scan the array before every addition. For reading I have an easy cheat, since 0 isn't a valid value, I'll know when I hit the last element. What I think will also help out a lot is some restructuring of the class that's being worked with to produce the number lists in the first place. It's a small class to begin with and I think I can reduce it down to a value type struct, by moving the functions into the container class. If all goes well, the whole mess should fold up into cache with a bit of room to spare. Then it should just come down to figuring out how many sets I can queue up from the database at a time.

              The true man wants two things: danger and play. For that reason he wants woman, as the most dangerous plaything.

              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