Really fast Loop ~Serial Ports

I have successfully managed to connect my bluetooth device to my pc via bluetooth serial port connection.
Reading data and writing also works fine, problem is that device is sending data with 900k bps speed.
So far this device i am using is just a test device and it is sending data like this:
abcdefghijklmnopqrstuvwxyz0
abcdefghijklmnopqrstuvwxyz1
abcdefghijklmnopqrstuvwxyz2
abcdefghijklmnopqrstuvwxyz3
abcdefghijklmnopqrstuvwxyz4

I made function that checkes if last digit is increased by one in regards to previous one.
When i put reading function in Update and checking function in new thread there is about 15-20% packet loss… Then what i did was put everything into fixedUpdate and increased speed of it in “edit-time”, and i managed to lower it but it just seemed like a bad way to do this.
After hours of searching i found a script where guy makes loop run really fast.
I used that script and managed to get my packet loss to about 5%… Problem is that i have to get it to 0%.

public void Connect()
    {
        sp = new SerialPort(@"\\.\" + port, 9600);
        Debug.Log ("Connection started");
        try
        {
            sp.Open(); // opens the connection
     
            connected = true;
        }
        catch(System.Exception e)
        {
            Debug.Log(e.ToString());
            connected = false;
        }
     
    }
    private void SuperFastLoop()
    {
        // We can't use Time.time which is a Unity API, instead we'll use this
        var time = System.DateTime.UtcNow.Ticks;
        const int oneSecond = 1;
        var count = 0;

        // This begins our Update loop
        while (true)
        {
            if (System.DateTime.UtcNow.Ticks - time >= oneSecond)
            {
                ThreadedUpdatedsPerSecond = count;
                count = 0;
                time = System.DateTime.UtcNow.Ticks;
            }
            data = sp.ReadLine(); 
            digit = Regex.Replace(data, "[^0-9]", "");
            if(digit.Length==1) 
                lastDigits.Add(digit);
         
            if(lastDigits.Count>500)
            {
                numBuffer++;
                lostPackets = 0;
            
[B]//this is for testing how many packets we lost[/B]

                for(int k=0;k<lastDigits.Count;k++)
                    checkArray.Add(lastDigits[k]);
                lastDigits.Clear();
                int temp = int.Parse(checkArray[0]);     
                for(int i=0;i<checkArray.Count;i++)
                { 
                    if(temp != int.Parse(checkArray[i]))
                    { 
                        try
                        {
                            temp=int.Parse(checkArray[i+1]);
                            lostPackets++;
                        }
                        catch(System.Exception e)
                        {} 
                    }
                    else
                        temp++;
                    if(temp == 10)
                        temp =  0;
                }
                //Debug.Log("-----  lost packets: "+ ((lostPackets*100.0f)/checkArray.Count)+"%");
                pLost += (lostPackets*100.0f)/checkArray.Count;
                //Debug.Log("Avarage lost percentage "+pLost/numBuffer+"%");
                checkArray.Clear();
                lostPackets = 0;             
                    //thread = new Thread(checkPackets);
                    //thread.Start();             
            }
        count++;
          
 
        }
    }
    public void startStream()
    {
        RestartValues();
        sendCommand("start");
        _updateThread = new Thread(SuperFastLoop);
        _updateThread.Start();
      
    }

So now i am wondering how can i make this run even faster…
Any help would be much appreciated.

could try with deep profiler to see whats happening…

but some random things,

  • maybe could read larger chunk at a time, instead of readline?
  • regex is slow
  • using lists is slower than queue or array
  • with for loops can get length once, for(int k=0,len=lastDigits.Count;k<len;k++)
  • no need int.parse, can compare characters instead?
  • try catch is slow, can use int.tryParse to check if can convert
  • could have another thread to do data processing / checking

It is worth to mention that, i tryied deleting checking part, and I went through array by myself, and I still found few errors, so i guess checking function doesnt make any difference.

What are some other ways of reading from SP ? Other then readLine.

not even sure, with tcp sockets can get callback events when data is available…

maybe this,
https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readexisting(v=vs.110).aspx

then could look into some of these to process string,
http://stackoverflow.com/questions/8793762/what-is-the-fastest-way-to-iterate-through-individual-characters-in-a-string-in

May I add that first 300-400 recieved lines of data are always 100% correct… And after some point packets are starting to get weird…

abcdefghijklmnopqrstuvwxyz2
abcdefghijklmnopqrstuvwxyz3
abcdefghijklmnopqrstuvwxyz4
abcdefghijklmnopqrstuvwxyz5
aopqrstuvwxyz2
abcdefghijklmnopqrstuvwxyz3
abcdefghijklmnopqrstuvwxyz4
abcdefghijklmnopqrstuvwxyz5
abcdefghijklmnopqrstuvwxyz6

This first happens on line 250 and it becomes more frequent after that.