Cederic Posted December 27, 2008 Share Posted December 27, 2008 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? Link to comment Share on other sites More sharing options...
FireFox Posted December 27, 2008 Share Posted December 27, 2008 @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... Cheers, FireFox. Link to comment Share on other sites More sharing options...
FireFox Posted December 27, 2008 Share Posted December 27, 2008 @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. Link to comment Share on other sites More sharing options...
TurionAltec Posted December 27, 2008 Share Posted December 27, 2008 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][ÝÊ Link to comment Share on other sites More sharing options...
TurionAltec Posted December 27, 2008 Share Posted December 27, 2008 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") Link to comment Share on other sites More sharing options...
Zedna Posted December 28, 2008 Share Posted December 28, 2008 Just try to measure times of differrent methods yourself to see which method is quicker. Use this: $start = TimerInit() ; some code ... ConsoleWrite(TimerDiff($start)) Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
Cederic Posted December 28, 2008 Author Share Posted December 28, 2008 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now