Jump to content

Optimize my script


 Share

Recommended Posts

At first, hey I am Ole and I am new here, hello.  :bye: My English isn't very neat so forgive me for that.

I wrote a script to run length decode a file which has been loaded into memory. All is working perfect, but, it clocks in at around 17 seconds on my machine. While that wouldn't be such a big problem for someone else, for me it is... I am relatively new to AutoIt so maybe some folks could probably hint me on stuff to read through and things I should really know. The reason behind all this... I just have the idea it could be improven much more.

So here is my script

; Containing byte array struct, created in another function not showed here
Global $FILE

; Reserve 2065676 bytes for decoded file
Global $DECODED = DllStructCreate("byte[" & 2065676 & "]")

; Well...
Func cwln($s)
   Return ConsoleWrite($s & @lf)
EndFunc

; Converts unsigned byte to signed byte
Func SByteValue($b)
   If BitAND($b, 0x80) == 0x80 Then
      Return -128 + BitXOR($b, 0x80)
   Else
      Return $b
   EndIf
EndFunc

; Decode function
Func Decode()
   
   ; Keep track of several things
   Local $i = 1, $end = 0, $bVal = 0, $dIndex = 1
   Local $bench = TimerInit()
   
   ; Start decode loop
   cwln("Run length decoding...")
   
   While $i <= DllStructGetSize($FILE) - 4
      
      ; Initialized to find out if it has a sign bit set
      $bVal = SByteValue(DllStructGetData($FILE, 1, $i))
      
      ; Decoding
      If $bVal < 0 Then
         $end = 1 - $bVal
         $i += 1
         For $j = 1 To $end
            DllStructSetData($DECODED, 1, DLLStructGetData($FILE, 1, $i), $dIndex)
            $dIndex += 1
         Next
      Else
         $end = $bVal + 1
         For $k = 1 To $end
            $i += 1
            DllStructSetData($DECODED, 1, DLLStructGetData($FILE, 1, $i), $dIndex)
            $dIndex += 1
         Next
      EndIf
      
      $i += 1
   WEnd
   
   cwln("Run length decoding ended in " & Ceiling(TimerDiff($bench) / 1000) & " seconds.")
   
EndFunc

Decode()

So to clarify on a point... it is working well, no bugs or whatsoever, it just annoys me it is this slow. If something is not clear, tell me, I will try to clarify :) byebye

Edit: I realize the title looks somewhat imperative... well it isn't meant to be

Edited by Ole
Link to comment
Share on other sites

I would replace

While $i <= DllStructGetSize($FILE) - 4

with

$iSize = DllStructGetSize($FILE) - 4
While $i <= $iSize

Saves you calling DllStuctGetSize over and over again.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Thanks for the fast reply. I tried your correction, which I will remember as common sense, although it does not seem to help very much. Benching te seperate parts (converting, decoding) tells me the decode process takes twice as much time as the converting. I will first try to edit the converting part so that it won't be needed anymore. Any suggestions on how to speed up the decoding loops are very welcome. Thanks in advance!

Edit: I tried using native array implementation, which was very slow, then the Binary constructor which led me to hell, then the structs which are fast, maybe there is something faster?

Edited by Ole
Link to comment
Share on other sites

Oh wow thanks Zedna, that's some useful info. It seems I used the arrays wrong, they are twice as fast as structs when initializing. I allready chipped off some seconds by replacing the ubyte to sbyte conversion function to some simple in-loop calculations.

I am going to try the array thing again... think I used it wrong. But I am curious if the reading would be as fast as the struct thing. Also how could I use an array as buffer for _WinAPI_ReadFile?

Link to comment
Share on other sites

Array use seemed much faster after all. Thanks for the tips! I am still curious about using an array as buffer for _WinAPI_ReadFile or another method to do this. That is because I am now converting te struct to array after buffering which takes extra time.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...