Jump to content

Whoa, this function is SLOW...


Recommended Posts

OK, im new to AutoIt but not a complete newbie when it comes to programming.

I have to admit that so far I LOVE this application... the language has been easy to learn and the results have been impressive... except for one thing... speed...

I am working on my first project i just finished a large part of it. But when i tested out the speeds I found it TERRIBLY SLOW. I mean i expected it to not run blazing fast or anything, but its literally 10 times slower then what i thought it might be.

Since I am new to this specific programming environment I realize that there must be something I have done to slow it down... but I can not find it.

Here is a section of code:

CODE

#include <Array.au3>

;Dim $xDeck[52]

;$Scores = _ArrayCreate( 4,5,9,3,5,11,12)

;$xTempDeck = CreateNewDeck()

$x = _ArrayCreate( "3:a","2:b","6:b","7:c","2:a","8:d","3:d" )

$sList = _ArrayCreate( "a","b","b","c","a","d","d" )

$vList = _ArrayCreate( "3","2","6","7","2","8","3" )

$xCycle = 10 ;2000

$begin = TimerInit()

For $i = 1 to $xCycle

MyNewFunction( $x, $sList, $vList )

Next

$dif = TimerDiff($begin)

MsgBox(0,"Time Difference",$dif)

Func MyNewFunction( $zHand, $zSuitList, $zvalList )

$cardsInHand = UBound($zSuitList)

Dim $tempList, $valHand

$tempList = _ArrayCreate("a", "b", "c", "d")

For $i = 0 to 3 Step 1

$xList = $zSuitList

$xTempHand = $zvalList

$valHand = _ArrayCreate(0, 0)

$x = 0

Do

If _xGetPos( $xList, $tempList[$i] ) <> -1 then

_ArrayAdd( $valHand, $xTempHand[ _xGetPos( $xList, $tempList[$i] ) ] )

EndIf

_ArrayDelete($xTempHand, _xGetPos( $xList, $tempList[$i] ) )

_ArrayDelete( $xList, _xGetPos( $xList, $tempList[$i] ) )

$x = $x + 1

Until _xGetPos( $xList, $tempList[$i] ) = -1

if $x >= 5 then

_ArraySort( $valHand, 1 )

$HC = $valHand[ 0 ]

$xScore = ( ($valHand[0]*1000000) + ($valHand[1]*100000000) + ($valHand[2]) + ($valHand[3]) + $valHand[4] )

if $score < $xScore then $score = $xScore

$pp = 0

For $xCount = 0 To 4

if (_xGetPos( $zHand, ($HC-$xCount)&":"&$tempList[$i] ) <> -1) then $pp = $pp + 1

Next

if ($pp < 5) and ($HC = 14) then

$pp = 0

if (_xGetPos( $zHand, "14:"&$tempList[$i] ) <> -1) then $pp = 1

For $xCount = 1 To 4

if (_xGetPos( $zHand, ($xCount+1)&":"&$tempList[$i] ) <> -1) then $pp = $pp + 1

Next

if $pp = 5 then

$xScore = 500000000000

if $score < $xScore then $score = $xScore

Endif

Elseif $pp = 5 then

$score = ( $HC * 100000000000 )

endif

endIf

Next

EndFunc

Func _xGetPos( $xArray, $xNum )

;Find and see if $xNum is in array $avArray, if it is return loc, if not, return -1

For $i = 0 To ( UBound( $xArray ) - 1 )

if $xNum = $xArray[$i] then

Return $i

EndIf

Next

REturn -1

EndFunc

The odd thing about this specific function... when i pull it from the overall project it and run it exactly as seen above, it finishes out at around 150 (or so that is what is printed in the msg window, so what is that, .15 of a second)....

But when it is embedded into the overall project, it adds a total time of 1600 (or i guess 1.6 seconds)...

So, first off, why would this function run 1/10th the speed once it is embedded in an overall project?

Secondly, is there anything you can see in it that would be an obvious flag for slowing thigns down?

Much much appreciated as im pulling my hair out! :)

aajames

Link to comment
Share on other sites

I have found this speed issue too. Especially using large arrays things seem to take longer. I was trying to parse a 400,000 line text file: in AutoIt it took about 2 minutes, in Excel VBA it takes about 10 seconds. AutoIt is, like you said, still extremely useful however... speed is just something to keep in mind! If anyone has any tips on speed/performance they would be greatly appreciated. The only one I heard is that Case/Select statements are faster than elsif. Anyone have anything beyond that?

Kevin

Link to comment
Share on other sites

One more thing real quick. Everytime someone asks these speed questions it seems like people always need to know what the code does... this type of help is useful, but what about the relative speeds of built-in functions? What about simple loop optimizing? Is there a repository somewhere that has GENERAL information on AutoIt efficiencies?

Kevin

Link to comment
Share on other sites

So Case/Select will run faster then an ElseIf?

I am all for optimising the code as much as possible, but I am facing the issue of wanting to cut the speed down by 90%... literally... :)

The strange thing is... the function i listed in my first post - when run as shown - runs 10 TIMES FASTER then it does when embedded (as is) into the overall project.

Any thoughts on how that possible??? Possibly its a memory issue.

aajames

Link to comment
Share on other sites

Any thoughts on how that possible??? Possibly its a memory issue.

<{POST_SNAPBACK}>

I seem to recall performance issues in the past w/ Ubound() -- when I ran into that (about two years ago) , I began populating $array[0] with array length instead...Haven't been back to Ubound since, so don't know if that is still problematic speedwise..... Also if you are concatenating variables in a loop things will bog down immensely. Memory may well be an issue, as could file i/o operations. As a previous poster mentioned - as a previous poster mentioned, it would be worthwhile to check your times per iteration.

You may also wish to: examine any 'off the shelf' udfs you're using for any extra functionality. code yourself a more efficient sorting algortithm, or switch to a different language.

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

We have recently gotten around this problem in the beta. It was turning out that entire array was being copied, which slowed things down more and more as arrays got larger. Now we don't copy until necessary to make arrays unique. Take a look at the latest beta version.

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

The first thing I notice is that you're using _ArrayAdd and _ArrayDelete in your loops. These are both very ugly, very slow functions to call like this. Do you realize that each time you call, you're passing the entire array to the function, it's creating a new array (copying all the old values) and then passing the whole new array back to you?

Those functions are OK to call once or twice, but if you're going to loop around them, you're much better off writing your own array management routines.

I prefer 1-based arrays, where is size is in the [0] element.

Consider this:

Dim $Array[100];if you know how many elements you'll need, put that number here, otherwise pick a big number - too big is better than too small
;right now, $Array[0]=0, which means there are 0 useful elements in this array

;let's add an element
$Array[0] = $Array[0] + 1
If $Array[0] > UBound(Array) Then ReDim $Array[$Array[0] + 20];when you redim, do it by a number bigger than 1, so that you don't have to do a lot of ReDims
$Array[$Array[0]] = $NewValue

;then to remove an element off the end is easy:
if $Array[0] > 0 then $Array[0] = $Array[0] - 1

;Deleting in the middle is more difficult.  I think the best way is to have a marker, and it your loops, if you encounter a marker, you ignore that element as if it wasn't there.

;When you're done working with the array (when you know it won't change size anymore) you can filter out the marked elements and trim the array down to size
Link to comment
Share on other sites

Thanks for the tips.

I will try out the beta and see if i notice any speed increases.

Also I will try to weed out the _ArrayAdd and _ArrayDelete commands.. im sure i can come up with ways to do it. I like your idea of just setting all the values to [0] and having $Array[0] keep track of the count. If i follow this routine then i shouldnt have to use the Ubound($array) function as well.

I will do this tonight and post any changes i see in speed.

Thanks again,

aajames

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...