Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the fastest method to calculate substring

I have a huge "binary" string, like: 1110 0010 1000 1111 0000 1100 1010 0111....

It's length is 0 modulo 4, and may reach 500,000.

I have also a corresponding array: {14, 2, 8, 15, 0, 12, 10, 7, ...}

(every number in the array corresponds to 4 bits in the string)

Given this string, this array, and a number N, I need to calculate the following substring string.substr(4*N, 4), i.e.:

for N=0 the result should be 1110

for N=1 the result should be 0010

I need to perform this task many many times, and my question is what would be the fastest method to calculate this substring ?

One method is to calculate the substring straight forward: string.substr(4*N, 4). I'm afraid this one is not efficient for such huge strings.

Another method is to use array[N].toString(2) and then wrap the result with zeros if needed. I'm not sure how fast is this.

May be you have any other ideas ?

like image 737
Misha Moroshko Avatar asked May 28 '10 13:05

Misha Moroshko


1 Answers

Where does the string come from? Why not represent the string not as binary, but as hex, and then you can store each four-binary-digit section as a single character? (You could obviously pack it twice that densely if you wanted, or actually now that I think of it, 4 times, since Javascript strings are 16-bit Unicode). Then finding a single group would be a single call to "charAt()", and you'd just have to expand to the binary form via a lookup table.

edit — oh well duhh, you already have an array. In that case don't do the substring work at all; it's crazy. Just grab the array element and translate it through a lookup array into the 4-binary-digit string.

like image 182
Pointy Avatar answered Sep 22 '22 03:09

Pointy