To string or not to string
-
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.
-
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.
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[^]
-
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[^]
nice find Mia - I'll remember that :-D
-
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[^]
-
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.
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
-
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
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.