1

so I am creating a piece of software that in short, has a list of original byte sequences and new sequences that those bytes need to be changed into, kinda like this in text form "original location(currently irrelevant as sequence can be in different places) $ 56,69,71,73,75,77 : 56,69,71,80,50,54"

I already have code that works fine, however there can be up to 600+ of these sequences to find and change and in some cases it is taking a really really long time 15 mins +, i think it is down to how long it is taking to find the sequences to them change so i am trying to find a better way to do this as currently it is unusable due to how long it takes.

I have copied the whole code for this function below in hopes one of you kind souls can have a look and help =)

Dim originalbytes() As Byte

Dim fd As OpenFileDialog = New OpenFileDialog() fd.Title = "Select the file" fd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" fd.FilterIndex = 2 If fd.ShowDialog() = DialogResult.OK Then TextBox2.Text = fd.FileName originalbytes = File.ReadAllBytes(fd.FileName) End If Dim x As Integer = 0 Dim y As Integer = 0 Dim textbox1array() = TextBox1.Lines Dim changedbytes() = originalbytes Dim startvalue As Integer = 0 Dim databoxarray() As String Dim databoxarray2() As String While x < textbox1array.Length - 1 'for each change to make databoxarray = textbox1array(x).Replace(" $ ", vbCr).Replace(" : ", vbCr).Split databoxarray2 = databoxarray(1).Replace(",", vbCr).Split Dim databox2bytes() As String = databoxarray2 'copy original bytes line to databox2 lines y = 0 While y < (originalbytes.Length - databox2bytes.Length) 'repeat for all bytes in ori file - size of data to find If originalbytes(y) = databox2bytes(0) Then startvalue = y Dim z As String = 1 Dim samebytecounter As Integer = 1 While z < databox2bytes.Length 'repeat for all ori bytes If originalbytes(y + z) = databox2bytes(z) Then samebytecounter = samebytecounter + 1 End If z = z + 1 End While If samebytecounter = databox2bytes.Length Then 'same original data found, make changes Dim bytestoinsert() As String = databoxarray(2).Replace(",", vbCr).Split Dim t As Integer = 0 While t < bytestoinsert.Length changedbytes(startvalue + t) = bytestoinsert(t) t = t + 1 End While End If End If y = y + 1 End While x = x + 1 End While File.WriteAllBytes(TextBox2.Text & " modified", changedbytes)

10
  • 2
    Any chance you could add some potential test data, and values that you enter in the different text boxes? I also don't see where you define originalbytes. If this is working code, maybe it would be better to post this question on codereview Commented Jul 1, 2017 at 10:22
  • sorry yes the original bytes is loaded as a byte array in another function, unfortunately i can't share one of the files as they are private =( Commented Jul 1, 2017 at 10:33
  • Could you at least share it's definition and object type? Commented Jul 1, 2017 at 10:34
  • this code is defiantly can be extremely improved, can you show us an example of the input and the requested output? also, do not use textbox, use a variable, writing to textbox is defecting efficiency . Commented Jul 1, 2017 at 10:35
  • added to the post the code where i defined the originabytes and get the data Commented Jul 1, 2017 at 10:40

1 Answer 1

1

Let 's take a look at that inner while loop in your code, there are some things that can be optimized:

There is no need to check the total length all the time

Dim length as Integer = originalbytes.Length - databox2bytes.Length
While y < length


    'repeat for all bytes in ori file - size of data to find
    If originalbytes(y) = databox2bytes(0) Then
        startvalue = y

z is not necessary, samebytecounter does exactly the same

        Dim samebytecounter As Integer = 1

This while loop is a real bottleneck, since you always check the full length of your databox2bytes, you should rather quit the while loop when they don't match

        While samebytecounter  < databox2bytes.Length AndAlso originalbytes(y + samebytecounter ) = databox2bytes(samebytecounter )
            samebytecounter = samebytecounter + 1
        End While

This seems fine, but you already splitted the data at the top of your while loop, so, no need to create another array that does the same operation again

        If samebytecounter = databox2bytes.Length Then
            'same original data found, make changes
            Dim t As Integer = 0
            While t < databoxarray2.Length
                changedbytes(startvalue + t) = databoxarray2(t)
                t = t + 1
            End While
        End If
    End If
    y = y + 1
End While

For the rest I would agree that the algorithm you created is hugely inefficient, theoretically your code could have been rewritten like eg: (didn't really test this code)

Dim text = System.Text.Encoding.UTF8.GetString(originalbytes, 0, originalbytes.Length)
dim findText = System.Text.Encoding.UTF8.GetString(stringToFind, 0, stringToFind.Length)
dim replaceWith = System.Text.Encoding.UTF8.GetString(stringToSet, 0, stringToSet.Length)

text = text.Replace( findText, replaceWith )

dim outbytes = System.Text.Encoding.UTF8.GetBytes(text)

which would probably be a huge time saver.

For the rest your code seems to be created in such a way that nobody will really understand it if it's laying around for a month or so, I would say, including yourself

Sign up to request clarification or add additional context in comments.

3 Comments

thank you very much, this has given me some great ideas and highlighted some issues with the original code, sorry for the messy code wasn't really meant for others to see and understand when i first did it =S
@TakeyaSaito You are welcome, if it helped you, you can mark it as an answer
@jonathana Yeah, though the bytes named variables are hugely disturbing in this code, it makes it really hard what is really in there when they are defined as a string array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.