Results 1 to 7 of 7

Thread: Challenge: Beat these times!

  1. #1
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170

    Challenge: Beat these times!

    This time it's not a poll nor question here. But some challenge!

    Imagine we have some Long or Dword we get passed from somewhere and have to split this into 3 Bytes...

    The idea is from Maxer73's test-script- he wants to parse some parameters he receives from midi- and he wants it real fast. Now we got a few different attempts to split a number from Long to 3 Bytes (RBG-Reverse-Function). See this demonstration script (by Maxer73) - run it, read to see what it does - not much: splits up a few variables different ways - all the same result but different execution-times...

    Which - you guess is the fastest way?


    Method 1: Shifting the Bytes?
    Method 2: Division?
    Method 3: Peeking the bytes from memory at varptr?
    Method 4: MKL$ from Value and Peek Bytes from StrPtr ?
    Method 5: MKL$ and place virtual variable of adequate Type over it?


    Uses "Console"   
    
    %MAX_Tests = 75000
    Type data
      byte0 As Byte
      byte1 As Byte  
      byte2 As Byte
    End Type   
    
    Dim Midi As data 
    Long MidiWord 
    Double tStart,tEnd,tTime       'for obtain elapsed time 
    
    '---------------------------------------------------------------------
    PrintL "MidiWord conversion speed test" + Str$(%Max_Tests) + " of executions:"
    PrintL
    PrintL "Press a Key to start method Midi_Decode_Max"
    WaitKey  
    tStart = Timer
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Max (MidiWord)   
       PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2 
    Next  
    tEnd = Timer : tTime = (tEnd-tStart) 
    PrintL CRLF & "Executed in " & tTime & " seconds" & CRLF
    
    '---------------------------------------------------------------------  
    PrintL "Press a Key to start method Midi_Decode_Rene1"
    WaitKey    
    tStart = Timer
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Rene1(MidiWord) 
       PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next 
    tEnd = Timer : tTime = (tEnd-tStart) 
    PrintL CRLF & "Executed in " & tTime & " seconds" & CRLF
    
    '---------------------------------------------------------------------
    PrintL "Press a Key to start method Midi_Decode_Rene2"
    WaitKey 
    tStart = Timer
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Rene2(MidiWord) 
       PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next 
    tEnd = Timer : tTime = (tEnd-tStart) 
    PrintL CRLF & "Executed in " & tTime & " seconds" & CRLF
    
    '---------------------------------------------------------------------
    PrintL "Press a Key to start method Midi_Decode_Rene3"
    WaitKey   
    tStart = Timer
    For MidiWord = 1 To %Max_Tests 
       Midi_Decode_Rene3(MKL$(MidiWord)) 
       PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next 
    tEnd = Timer : tTime = (tEnd-tStart) 
    PrintL CRLF & "Executed in " & tTime & " seconds" & CRLF
    '---------------------------------------------------------------------
    
    PrintL "Press a Key to start method Midi_Decode_Rene4"
    WaitKey   
    tStart = Timer
    For MidiWord = 1 To %Max_Tests    
       Midi_Decode_Rene4(MKL$(MidiWord)) 
       PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next 
    tEnd = Timer : tTime = (tEnd-tStart) 
    PrintL CRLF & "Executed in " & tTime & " seconds" & CRLF
    
    '---------------------------------------------------------------------
    PrintL "Press a Key to exit"
    WaitKey 
    
    
    Sub Midi_Decode_Max (ByVal nVal As Long)        
       Dim temp0,temp1,temp2 As Long = nVal   
       
       Midi.byte0 = temp0 And 255    
       Midi.byte1 = (SHIFT SIGNED RIGHT temp1, 8) And 255 
       Midi.byte2 = (SHIFT SIGNED RIGHT temp2, 16) And 255       
    End Sub    
     
    Sub Midi_Decode_Rene1(ByVal anyValue As Long)   
       Midi.byte2 = Int(anyValue / &H10000)
       Midi.byte1 = Int((anyValue - &H10000 * Midi.byte2)/ &H100)
       Midi.byte0 = anyValue - Midi.byte2 * &H10000 - Midi.byte1 * &H100
    End Sub    
    
    Sub Midi_Decode_Rene2(ByRef someVal As Long)
       Midi.byte0 = Peek(Byte,VarPtr(someVal))
       Midi.byte1 = Peek(Byte,VarPtr(someVal)+ 1)
       Midi.byte2 = Peek(Byte,Varptr(someVal)+ 2) 
    End Sub     
    
    Sub Midi_Decode_Rene3(ByVal someVal As String)
       Midi.byte0 = Peek(Byte,StrPtr(someVal))
       Midi.byte1 = Peek(Byte,StrPtr(someVal)+ 1)
       Midi.byte2 = Peek(Byte,StrPtr(someVal)+ 2) 
    End Sub  
    
    Sub Midi_Decode_Rene4(ByVal someVal As String)
     Local lByte As data At StrPtr(someVal)
     Midi = lByte
    End Sub
    
    You know any faster method?

    for comparison reasons (see image), comment lines 22, 33, 44, 55, 66 and set CONST %MAX_TESTS = 1000000
    Attached Images Attached Images
    Last edited by ReneMiner; 29-04-2013 at 21:38.
    I think there are missing some Forum-sections as beta-testing and support

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    I did a minor modification to the code. The Timer function is very approximate, for benchmarks it is better to use HiResTimer. After the modification, the script looks like:
    Uses "Console"  
     
    %MAX_Tests = 1000000
    Type data
      byte0 As Byte
      byte1 As Byte 
      byte2 As Byte
    End Type  
     
    Dim Midi As data
    Long MidiWord
    
    Quad tStart,tEnd
    Double tTime       'for obtain elapsed time
    
    ' -- Initialize high precision timer
    HiResTimer_Init
                     
    '---------------------------------------------------------------------
    PrintL "MidiWord conversion speed test" + Str$(%Max_Tests) + " of executions:"
    PrintL
    PrintL "Press a Key to start method Midi_Decode_Max"
    WaitKey 
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Max (MidiWord)  
       ''PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next 
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
     
    '--------------------------------------------------------------------- 
    PrintL "Press a Key to start method Midi_Decode_Rene1"
    WaitKey   
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Rene1(MidiWord)
       ''PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
     
    '---------------------------------------------------------------------
    PrintL "Press a Key to start method Midi_Decode_Rene2"
    WaitKey
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Rene2(MidiWord)
       ''PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
     
    '---------------------------------------------------------------------
    PrintL "Press a Key to start method Midi_Decode_Rene3"
    WaitKey  
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Rene3(MKL$(MidiWord))
       '' PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
    '---------------------------------------------------------------------
     
    PrintL "Press a Key to start method Midi_Decode_Rene4"
    WaitKey  
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests   
       Midi_Decode_Rene4(MKL$(MidiWord))
       '' PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
     
    '---------------------------------------------------------------------
    PrintL "Press a Key to exit"
    WaitKey
     
     
    Sub Midi_Decode_Max (ByVal nVal As Long)       
       Dim temp0,temp1,temp2 As Long = nVal  
        
       Midi.byte0 = temp0 And 255   
       Midi.byte1 = (SHIFT SIGNED RIGHT temp1, 8) And 255
       Midi.byte2 = (SHIFT SIGNED RIGHT temp2, 16) And 255      
    End Sub   
      
    Sub Midi_Decode_Rene1(ByVal anyValue As Long)  
       Midi.byte2 = Int(anyValue / &H10000)
       Midi.byte1 = Int((anyValue - &H10000 * Midi.byte2)/ &H100)
       Midi.byte0 = anyValue - Midi.byte2 * &H10000 - Midi.byte1 * &H100
    End Sub   
     
    Sub Midi_Decode_Rene2(ByRef someVal As Long)
       Midi.byte0 = Peek(Byte,VarPtr(someVal))
       Midi.byte1 = Peek(Byte,VarPtr(someVal)+ 1)
       Midi.byte2 = Peek(Byte,VarPtr(someVal)+ 2)
    End Sub    
     
    Sub Midi_Decode_Rene3(ByVal someVal As String)
       Midi.byte0 = Peek(Byte,StrPtr(someVal))
       Midi.byte1 = Peek(Byte,StrPtr(someVal)+ 1)
       Midi.byte2 = Peek(Byte,StrPtr(someVal)+ 2)
    End Sub 
     
    Sub Midi_Decode_Rene4(ByVal someVal As String)
     Local lByte As data At StrPtr(someVal)
     Midi = lByte
    End Sub
    
    ... and the results on my precious Core i5-3350P are the following:
    MidiWord conversion speed test 1000000 of executions:

    Press a Key to start method Midi_Decode_Max

    Executed in 7.38 seconds

    Press a Key to start method Midi_Decode_Rene1

    Executed in 3.36 seconds

    Press a Key to start method Midi_Decode_Rene2

    Executed in 3.01 seconds

    Press a Key to start method Midi_Decode_Rene3

    Executed in 3.91 seconds

    Press a Key to start method Midi_Decode_Rene4

    Executed in 4.58 seconds

    Press a Key to exit

    Petr
    Last edited by Petr Schreiber; 29-04-2013 at 23:01.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Location
    Induno Olona (VA) Italy
    Age
    50
    Posts
    15
    Rep Power
    16

    Midi Decode Word

    Hi Petr....

    Thanks for your reply, the script work very well with HiRes Timer
    and is more precise.

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Here is my method.

    Hoping data is correct ... It seems I got a nice speed improvement: 8 to 20 times faster than any other method.

    It uses a UNION data structure in order to directly assign a LONG and have it split into needed part automatically.
    This method avoid to call a SUB/FUNCTION and instead assign the LONG directly to data and UNION automagically split it into relevant part.
    Calling SUB/FUNCTION requires a lot of time if there are local variables or parameters due to local stack allocation.

    MidiWord conversion speed test 1000000 of executions:


    Press a Key to start method Midi_Decode_Max
    Executed in 9.40 seconds


    Press a Key to start method Midi_Decode_Rene1
    Executed in 4.06 seconds


    Press a Key to start method Midi_Decode_Rene2
    Executed in 3.75 seconds


    Press a Key to start method Midi_Decode_Rene3
    Executed in 4.66 seconds


    Press a Key to start method Midi_Decode_Rene4
    Executed in 5.88 seconds


    Press a Key to start method Midi_Decode_Eros1
    Executed in 0.40 seconds


    Press a Key to exit
    Uses "Console" 
      
    %MAX_Tests = 1000000
    Type data
      byte0 As Byte
      byte1 As Byte
      byte2 As Byte
    End Type 
    
    
    Union uData
      AsByte As data
      AsLong As Long
    End Union
    
    
    Type tData
      Element As uData
    End Type
    
    
      
    Dim Midi As data
    Long MidiWord
     
    Quad tStart,tEnd
    Double tTime       'for obtain elapsed time
     
    ' -- Initialize high precision timer
    HiResTimer_Init
                      
    '---------------------------------------------------------------------
    PrintL "MidiWord conversion speed test" + Str$(%Max_Tests) + " of executions:"
    PrintL
    PrintL "Press a Key to start method Midi_Decode_Max"
    WaitKey
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Max (MidiWord)  
       ''PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
      
    '--------------------------------------------------------------------- 
    PrintL "Press a Key to start method Midi_Decode_Rene1"
    WaitKey  
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Rene1(MidiWord)
       ''PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
      
    '---------------------------------------------------------------------
    PrintL "Press a Key to start method Midi_Decode_Rene2"
    WaitKey
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Rene2(MidiWord)
       ''PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
      
    '---------------------------------------------------------------------
    PrintL "Press a Key to start method Midi_Decode_Rene3"
    WaitKey 
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Rene3(MKL$(MidiWord))
       '' PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
    '---------------------------------------------------------------------
      
    PrintL "Press a Key to start method Midi_Decode_Rene4"
    WaitKey 
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests   
       Midi_Decode_Rene4(MKL$(MidiWord))
       '' PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
      
    '---------------------------------------------------------------------
    PrintL "Press a Key to start method Midi_Decode_Eros1"
    WaitKey  
    tStart = HiResTimer_Get
    Dim lData As tData
    For MidiWord = 1 To %Max_Tests    
       lData.Element.AsLong = MidiWord
       'PrintL lData.Element.AsByte.byte0 & $TAB & lData.Element.AsByte.byte1 & $TAB & lData.Element.AsByte.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
    '---------------------------------------------------------------------
    
    
    PrintL "Press a Key to exit"
    WaitKey
      
      
    Sub Midi_Decode_Max (ByVal nVal As Long)       
       Dim temp0,temp1,temp2 As Long = nVal  
         
       Midi.byte0 = temp0 And 255   
       Midi.byte1 = (SHIFT SIGNED RIGHT temp1, 8) And 255
       Midi.byte2 = (SHIFT SIGNED RIGHT temp2, 16) And 255      
    End Sub  
       
    Sub Midi_Decode_Rene1(ByVal anyValue As Long)  
       Midi.byte2 = Int(anyValue / &H10000)
       Midi.byte1 = Int((anyValue - &H10000 * Midi.byte2)/ &H100)
       Midi.byte0 = anyValue - Midi.byte2 * &H10000 - Midi.byte1 * &H100
    End Sub  
      
    Sub Midi_Decode_Rene2(ByRef someVal As Long)
       Midi.byte0 = Peek(Byte,VarPtr(someVal))
       Midi.byte1 = Peek(Byte,VarPtr(someVal)+ 1)
       Midi.byte2 = Peek(Byte,VarPtr(someVal)+ 2)
    End Sub   
      
    Sub Midi_Decode_Rene3(ByVal someVal As String)
       Midi.byte0 = Peek(Byte,StrPtr(someVal))
       Midi.byte1 = Peek(Byte,StrPtr(someVal)+ 1)
       Midi.byte2 = Peek(Byte,StrPtr(someVal)+ 2)
    End Sub
      
    Sub Midi_Decode_Rene4(ByVal someVal As String)
     Local lByte As data At StrPtr(someVal)
     Midi = lByte
    End Sub
    
    Last edited by ErosOlmi; 30-04-2013 at 07:44.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    I got inspired by Eros and used SetAt instead, the result is sometimes liiiiiitle bit faster :P

    Test this time on Intel Core 2 Duo T6600 @ 2.2GHz:
    MidiWord conversion speed test 1000000 of executions:

    Press a Key to start method Midi_Decode_Max

    Executed in 22.21 seconds

    Press a Key to start method Midi_Decode_Rene1

    Executed in 10.07 seconds

    Press a Key to start method Midi_Decode_Rene2

    Executed in 8.12 seconds

    Press a Key to start method Midi_Decode_Rene3

    Executed in 11.36 seconds

    Press a Key to start method Midi_Decode_Rene4

    Executed in 13.69 seconds

    Press a Key to start method Midi_Decode_Eros1

    Executed in 0.83 seconds

    Press a Key to start method Midi_Decode_Petr1

    Executed in 0.82 seconds

    Press a Key to exit
    Code:
    Uses "Console"
       
    %MAX_Tests = 1000000
    Type data
      byte0 As Byte
      byte1 As Byte
      byte2 As Byte
    End Type
     
     
    Union uData
      AsByte As data
      AsLong As Long
    End Union
     
     
    Type tData
      Element As uData
    End Type
     
     
       
    Dim Midi As data
    Long MidiWord
      
    Quad tStart,tEnd
    Double tTime       'for obtain elapsed time
      
    ' -- Initialize high precision timer
    HiResTimer_Init
                       
    '---------------------------------------------------------------------
    PrintL "MidiWord conversion speed test" + Str$(%Max_Tests) + " of executions:"
    PrintL
    PrintL "Press a Key to start method Midi_Decode_Max"
    WaitKey
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Max (MidiWord) 
       ''PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
       
    '---------------------------------------------------------------------
    PrintL "Press a Key to start method Midi_Decode_Rene1"
    WaitKey 
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Rene1(MidiWord)
       ''PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
       
    '---------------------------------------------------------------------
    PrintL "Press a Key to start method Midi_Decode_Rene2"
    WaitKey
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Rene2(MidiWord)
       ''PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
       
    '---------------------------------------------------------------------
    PrintL "Press a Key to start method Midi_Decode_Rene3"
    WaitKey
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Rene3(MKL$(MidiWord))
       '' PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
    '---------------------------------------------------------------------
       
    PrintL "Press a Key to start method Midi_Decode_Rene4"
    WaitKey
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests  
       Midi_Decode_Rene4(MKL$(MidiWord))
       '' PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
       
    '---------------------------------------------------------------------
    PrintL "Press a Key to start method Midi_Decode_Eros1"
    WaitKey 
    tStart = HiResTimer_Get
    Dim lData As tData
    For MidiWord = 1 To %Max_Tests   
       lData.Element.AsLong = MidiWord
       'PrintL lData.Element.AsByte.byte0 & $TAB & lData.Element.AsByte.byte1 & $TAB & lData.Element.AsByte.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
    '---------------------------------------------------------------------       
    PrintL "Press a Key to start method Midi_Decode_Petr1"
    WaitKey 
    tStart = HiResTimer_Get
    Dim petr1Data As data At 0
    For MidiWord = 1 To %Max_Tests   
       SetAt(petr1Data, VarPtr(midiWord))
       ''PrintL petr1Data.byte0 & $TAB & petr1Data.byte1 & $TAB & petr1Data.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
    '---------------------------------------------------------------------
     
     
    PrintL "Press a Key to exit"
    WaitKey
       
       
    Sub Midi_Decode_Max (ByVal nVal As Long)      
       Dim temp0,temp1,temp2 As Long = nVal 
          
       Midi.byte0 = temp0 And 255  
       Midi.byte1 = (SHIFT SIGNED RIGHT temp1, 8) And 255
       Midi.byte2 = (SHIFT SIGNED RIGHT temp2, 16) And 255     
    End Sub 
        
    Sub Midi_Decode_Rene1(ByVal anyValue As Long) 
       Midi.byte2 = Int(anyValue / &H10000)
       Midi.byte1 = Int((anyValue - &H10000 * Midi.byte2)/ &H100)
       Midi.byte0 = anyValue - Midi.byte2 * &H10000 - Midi.byte1 * &H100
    End Sub 
       
    Sub Midi_Decode_Rene2(ByRef someVal As Long)
       Midi.byte0 = Peek(Byte,VarPtr(someVal))
       Midi.byte1 = Peek(Byte,VarPtr(someVal)+ 1)
       Midi.byte2 = Peek(Byte,VarPtr(someVal)+ 2)
    End Sub  
       
    Sub Midi_Decode_Rene3(ByVal someVal As String)
       Midi.byte0 = Peek(Byte,StrPtr(someVal))
       Midi.byte1 = Peek(Byte,StrPtr(someVal)+ 1)
       Midi.byte2 = Peek(Byte,StrPtr(someVal)+ 2)
    End Sub
       
    Sub Midi_Decode_Rene4(ByVal someVal As String)
     Local lByte As data At StrPtr(someVal)
     Midi = lByte
    End Sub
    
    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  6. #6
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    Pretty fast methods- but Max wants to have the values stored to "midi"-variable instantly - and he does not want to change anything about the data-type.

    So it has to print out this:

    PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2

    but your both ways don't show the expected numbers if I insert this line. So there is some conversion in your code needed as
    "Midi = ..."

    which you have not timed in your test

    How about this:
    '...
    PrintL "Press a Key to start method Midi_Decode_Rene5"
    WaitKey
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests  
       Poke$(VarPtr(midi), LEFT$(MKL$(MidiWord),3))
    
       'PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
    '---------------------------------------------------------------------
    
    Last edited by ReneMiner; 30-04-2013 at 13:54.
    I think there are missing some Forum-sections as beta-testing and support

  7. #7
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    You are right. Then...

    Poke$? That is soooo 80's BASIC

    Lets see what new ThinBASIC offers, it's time to release the Kraken... or Memory_Copy!:
    Uses "Console"
       
    %MAX_Tests = 1000000
    Type data
      byte0 As Byte
      byte1 As Byte
      byte2 As Byte
    End Type
     
     
    Union uData
      AsByte As data
      AsLong As Long
    End Union
     
     
    Type tData
      Element As uData
    End Type
     
     
       
    Dim Midi As data
    Long MidiWord
      
    Quad tStart,tEnd
    Double tTime       'for obtain elapsed time
      
    ' -- Initialize high precision timer
    HiResTimer_Init
                       
    '---------------------------------------------------------------------
    PrintL "MidiWord conversion speed test" + Str$(%Max_Tests) + " of executions:"
    PrintL
    PrintL "Press a Key to start method Midi_Decode_Max"
    WaitKey
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Max (MidiWord) 
       ''PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
       
    '---------------------------------------------------------------------
    PrintL "Press a Key to start method Midi_Decode_Rene1"
    WaitKey 
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Rene1(MidiWord)
       ''PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
       
    '---------------------------------------------------------------------
    PrintL "Press a Key to start method Midi_Decode_Rene2"
    WaitKey
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Rene2(MidiWord)
       ''PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
       
    '---------------------------------------------------------------------
    PrintL "Press a Key to start method Midi_Decode_Rene3"
    WaitKey
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests
       Midi_Decode_Rene3(MKL$(MidiWord))
       '' PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
    '---------------------------------------------------------------------
       
    PrintL "Press a Key to start method Midi_Decode_Rene4"
    WaitKey
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests  
       Midi_Decode_Rene4(MKL$(MidiWord))
       '' PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
       
    '---------------------------------------------------------------------
    PrintL "Press a Key to start method Midi_Decode_Eros1"
    WaitKey 
    tStart = HiResTimer_Get
    Dim lData As tData
    For MidiWord = 1 To %Max_Tests   
       lData.Element.AsLong = MidiWord
       ''PrintL lData.Element.AsByte.byte0 & $TAB & lData.Element.AsByte.byte1 & $TAB & lData.Element.AsByte.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
    '---------------------------------------------------------------------       
    PrintL "Press a Key to start method Midi_Decode_Petr1 Tuned"
    WaitKey 
    tStart = HiResTimer_Get
    For MidiWord = 1 To %Max_Tests   
       Memory_Copy(VarPtr(MidiWord), VarPtr(Midi), SizeOf(Midi))
       ''PrintL Midi.byte0 & $TAB & Midi.byte1 & $TAB & Midi.byte2
    Next
    tEnd = HiResTimer_Get : tTime = (tEnd-tStart)/1000000
    PrintL CRLF & "Executed in " & Format$(tTime, "#.00") & " seconds" & CRLF
    '---------------------------------------------------------------------
     
     
    PrintL "Press a Key to exit"
    WaitKey
       
       
    Sub Midi_Decode_Max (ByVal nVal As Long)      
       Dim temp0,temp1,temp2 As Long = nVal 
          
       Midi.byte0 = temp0 And 255  
       Midi.byte1 = (SHIFT SIGNED RIGHT temp1, 8) And 255
       Midi.byte2 = (SHIFT SIGNED RIGHT temp2, 16) And 255     
    End Sub 
        
    Sub Midi_Decode_Rene1(ByVal anyValue As Long) 
       Midi.byte2 = Int(anyValue / &H10000)
       Midi.byte1 = Int((anyValue - &H10000 * Midi.byte2)/ &H100)
       Midi.byte0 = anyValue - Midi.byte2 * &H10000 - Midi.byte1 * &H100
    End Sub 
       
    Sub Midi_Decode_Rene2(ByRef someVal As Long)
       Midi.byte0 = Peek(Byte,VarPtr(someVal))
       Midi.byte1 = Peek(Byte,VarPtr(someVal)+ 1)
       Midi.byte2 = Peek(Byte,VarPtr(someVal)+ 2)
    End Sub  
       
    Sub Midi_Decode_Rene3(ByVal someVal As String)
       Midi.byte0 = Peek(Byte,StrPtr(someVal))
       Midi.byte1 = Peek(Byte,StrPtr(someVal)+ 1)
       Midi.byte2 = Peek(Byte,StrPtr(someVal)+ 2)
    End Sub
       
    Sub Midi_Decode_Rene4(ByVal someVal As String)
     Local lByte As data At StrPtr(someVal)
     Midi = lByte
    End Sub
    
    Petr
    Last edited by Petr Schreiber; 30-04-2013 at 14:51.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. thinBASIC QB Compatibility Code Challenge
    By John Spikowski in forum General
    Replies: 0
    Last Post: 20-06-2009, 21:22
  2. AllBasic.Info Code Challenge
    By John Spikowski in forum Events
    Replies: 0
    Last Post: 08-08-2008, 11:16
  3. tbgl coding challenge
    By kryton9 in forum TBGL General
    Replies: 4
    Last Post: 10-06-2007, 21:42
  4. thinBASIC coding challenge #1
    By Petr Schreiber in forum Challenge
    Replies: 29
    Last Post: 10-04-2007, 21:20
  5. Equation solved 100000 times
    By ErosOlmi in forum Execution speed tests
    Replies: 6
    Last Post: 18-03-2007, 22:56

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •