Sign in to follow this  
Followers 0
Cederic

Optimizing Scripts - What are good habits for fast running code?

7 posts in this topic

Hello!

It could be argued that I am too tired to be writing forum posts, but I've never let that stop me before.

Okay, so I am pretty new to autoit, and a beginner in coding in general. Or slightly above, but still, pretty fresh meat. Have done a few scripts before and playing around with some now, done a bit of PHP and Java and the usual suspects, but pretty much low level programming covered in the first chapters of the books I should have probably read but didn't. So if by some freak accident I happen to sound like I know what I am talking about, assume I am deluded.

Now, my problem is knowledge, or a lack of it. I can pretty easily find my way around the help file and samples and google lets met find lots of information about what code to use in a certain scenario. I learned the magic of loops long ago and might be accurately described as having something of a loop-fetish. Gotta love something that saves lots of lines of programming in one fell swoop.

But, what I can not seem to find is any information about what is more efficient to use. I love puzzles so I am working on a script to solve a simple pattern puzzle, but my initial trials ran into an efficiency problem.

There are basically two ways to tackle the problem - either typing in the code for every single possible position that can lead to a pattern and the moves needed to make it so and have each move checked in turn - or make the script have a basic intelligence, find a small combination and then see if it can find more matching pieces to expand the possible pattern. The problem is... it needs to be fast.

One question in specific I am wondering about - is it faster to store constants that will be compared later into separate variables and compare them with separate if statements, or to store them in an array and compare them in a loop? I tend to go for the loop to save a bunch of repeated code, but is this the most efficient way in terms of fast execution?

Other related questions would be...

Does keeping the main functions in included files rather than the main file make a difference?

As part of the script I need to know the colour of one pixel on each "piece" of the puzzle. I use PixelGetColor for this... Would it be quicker to call it individually on the fly just when I need to know the colour of a certain piece, or would it be quicker to check the entire board, store it in a multidimensional array and then do later checks against the array? The board would need to be re-read occasionally as it changes.

When I think about it I might have an array fetish as well. They are almost as fun as databases.

On a half related note... is there an easy way to measure the time things take in the script? The next best thing to getting a good "optimized code guide" here would be to find a way so I could conduct some unscientific experiments...

So, what is it? What are your best tips for speeding up execution speed, what should I keep in mind to streamline this code and put some bounce in its step? What functions to avoid, what to use instead...

The only thing I've found in my searches is that FileReadLine is a bad idea... which is okay, because I don't do any file handling at all apart from the debug files I write, and that stuff is removed before I use the script so it would not have an effect. Are there any other posts I should read that speak specifically about good general practices for code optimization?

Share this post


Link to post
Share on other sites



@Cederic

You will advert me when you will have finish your novel... seriously can you explain your problem in 10lines because at midnight (france) im too tired to understand and read this... :o

:)

Cheers, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

@Cederic

Ok...I read your 'novel'

-About if there difference if functions are in main script or not, my answer is no because all includes are regrouped in main file

-About to store your pixel array you can do it with variables like this :

$array = "FIRST";Store first to $array variable
Msgbox(64, "", $array)

$array = "SECOND";$array is now second
MsgBox(64, "", $array)

And I think before starting to do game in autoit you should start with easier scripts...

Cheers, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

WRT pixelgetcolor, I think it would be quicker to store the result in a variable if that value is going to be referred to several times.

-I think making the code in arrays and loops will allow you to make changes much more efficiently, which is also important.

-I don't think include is any more efficient than putting it in the main file, just makes it easier to reuse functions in different scripts.

To time, use TimerInit and TimerDiff

Also Autoit scripts are single threaded, so on a dual core processor, it will effectively only run on one core, and can only use 50% of the total CPU power.

If you have a lot of computational steps, you might be able to split the task into two, and launch a new script to do half the computation, and communicate back and forth with a text file or something.

eg: script 1

;script1.au3
Run("script2.exe")
for $i=1 to 500
;computation
next
While Fileexist("output.ini")<>1
sleep(50)
Wend ;wait around until the other script is done
iniread("output.ini")
...oÝ÷ Ø Ý«ÞjÇ¢w_WºÚ"µÍÜØÜ]LÂÜ ÌÍÌOMLHÈLØÛÛ]][Û^[]Ü]J ][ÝÛÝ]][I][ÝÊ

Share this post


Link to post
Share on other sites

Edit isn't working right.

To wait for the second script to finish replace

;script1.au3
$pidscript2=Run("script2.exe")
for $i=1 to 500
;computation
next
ProcessWaitClose($pidscript2)
iniread("output.ini")

Share this post


Link to post
Share on other sites

Thank you all for some good ideas!

Those timer functions will be handy to benchmark the functions myself. I still wish there would have been a "best practices" tutorial available so I could read up on it rather than have to rediscover it myself. :)

Actually, rediscovering is pretty fun as well.

Share this post


Link to post
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
Sign in to follow this  
Followers 0