0

What is the fastest way to convert a String into an array of Short integers representing the character codes?

I am now using this but it can probably be much faster:

Dim shortsarray(mystring.Length - 1) as Short
For i As Integer = 0 To mystring.Length - 1
  shortsarray(i) = AscW(mystring.Chars(i))
Next

Thanks.

Peladao
  • 4,036
  • 1
  • 23
  • 43
  • 1
    What character encoding are you looking for? ASCII? And why are you looking for something "faster"? Are you having performance problems that you have made sure are in this bit of code? – Oded Apr 10 '12 at 19:20
  • @Oded I mean the Unicode values (as the Strings.AscW function returns) – Peladao Apr 10 '12 at 19:27
  • @Oded This code will be running a lot so for the sake of efficiency (and learning something as well) I like to know the fastest way – Peladao Apr 10 '12 at 19:28
  • @JonEgerton Doh, I read that as `Dim shortsarray(mystring.Length - 1) As New Short` – vcsjones Apr 10 '12 at 19:29
  • You are doing it wrong, in that case. When it comes to performance tuning, you need to measure, find the bottlenecks, fix them and repeat (until you have reached the threshold you needed). – Oded Apr 10 '12 at 19:29
  • 2
    `AscW` is implemented as `Return CInt(char)`. Do you really think that this can be improved? – Tim Schmelter Apr 10 '12 at 19:31
  • @Oded I know, I know, textbook.. but making simple small parts of code that will run often fast in the first place will save lots of performance tuning later, while not costing anything (save asking and explaining this question) – Peladao Apr 10 '12 at 19:33
  • @Peladao: By the way, you should always set [`Option Strict`](http://stackoverflow.com/questions/222370/option-strict-on-and-net-for-vb6-programmers). `shortsarray(i) = AscW(mystring.Chars(i))` should not compile since you're assigning an int to a short-array. And a `short` doesn't increase performance at all, the opposite may be true. http://stackoverflow.com/questions/129023/net-integer-vs-int16 – Tim Schmelter Apr 10 '12 at 22:09
  • @Tim Good point, I was using Short to save space but it may not be worth the performance loss you are hinting at. – Peladao Apr 11 '12 at 07:34

2 Answers2

1

I do not know what kind of problem you are trying to solve. However, as an alternative, you could convert the string to an array of characters

Dim chars() As Char = mystring.ToCharArray()
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0
Dim shortsarray(mystring.Length - 1) as Short //One calculation
For i As Integer = 0 To mystring.Length - 1   //One calculation, repeats n times
  shortsarray(i) = AscW(mystring.Chars(i))    //Three calculations, repeats n times
Next

As a result, this should run in O(3n+1), or O(n). This is linear on the length of the input, and there isn't much improvement you can expect from that since you're doing a character-wise conversion. I think this is probably as good as you can expect, though there may be a library that will just convert the whole word in one go, making your code cleaner.

If you need performance increases, you want to first profile your whole program. This snippet may not be the problem.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102