|
-
May 14th, 2016, 10:03 AM
#23
Re: Trying to understand Collections
Out of curiosity, and since the first element is always an integer, suppose we use: -
Code:
Sub Get_em(ByVal WrdIn As String)
Dim Num As Integer, Wrd As String
Num = Val(WrdIn)
Wrd = WrdIn.Substring(InStr("|"))
End Sub
(yeah I know it doesn't go anywhere)
Would that cost us more or less ?
(If you have the time to think about it)
Think it through. Val() can't be doing anything more magic than Integer.Parse(). Its behavior is:
Code:
* Ignore whitespace characters.
* When a digit is found, start looking for a non-digit.
* Do the standard integer parse algorithm on the digits.
Let's use the input string "12345|John Smith", because my original sample string would fail your algorithm 
- Val() is going to have to iterate past "12345" until it reaches |. 6 steps.
- Then it has to read backwards to perform integer parsing, 3 steps per digit. 15 steps.
- And we've also had to allocate an Integer, 4 bytes.
After that, InStr() has to resolve. It has to start at the front and iterate to the "|", which is 6 steps. 12 if we count the comparison, but we haven't so far so let's not start.
It allocates the integer 5 as its return value. (Technically this is then copied onto the parameter stack but I haven't been paying attention to that either.) 4 more bytes.
Now an array with 10 elements is allocated (20 bytes), and 10 iterations happen to copy it. (10 steps)
So we weigh in at 6 + 15 + 6 + 10 = 37 steps, and 4 + 4 + 20 = 28 bytes. That's closer to the 24 bytes the class took, but 37 steps compared to 2 is a lot of wasted time.
Due to the immutability of strings, and the iterative nature of parsing values, the only way I can imagine string storage being as time efficient is "I want to store 10 1-digit Integers in a 10-character string." Though one might point out you can pack a 1-digit integer into a 3-bit number, so the 10 numbers would be 30 bits (4 bytes with 2 unused bits). The string would be 20 bytes. You can't win, with conversions.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|