# Speed it up and more efficient

## Recommended Posts

Does anyone have any ideas for speeding this up or making it more efficient?

```While 1
if \$input[4]<200 and \$fire1=1 Then
MouseUp("left")
\$fire1=0
EndIf
if \$input[4]>200 and \$fire1=0 then
MouseDown ( "left" )
\$fire1=1
EndIf
if \$input[3]<200 and \$fire2=1 Then
MouseUp("right")
\$fire2=0
EndIf
if \$input[3]>200 and \$fire2=0 then
MouseDown ( "right" )
\$fire2=1
EndIf
If \$input[6]<22000 and \$forward = 1 then
Send("{w up}")
\$forward=0
EndIf
If \$input[6]>22000 and \$forward = 0 then
Send("{w down}")
\$forward=1
EndIf
If \$input[6]>-22000 and \$back = 1 then
Send("{s up}")
\$back=0
EndIf
If \$input[6]<-22000 and \$back = 0 then
Send("{s down}")
\$back=1
EndIf
If \$input[5]<22000 and \$left = 1 then
Send("{a up}")
\$left=0
EndIf
If \$input[5]>22000 and \$left = 0 then
Send("{s down}")
\$left=1
EndIf
If \$input[5]>-22000 and \$right = 1 then
Send("{d up}")
\$right=0
EndIf
If \$input[5]<-22000 and \$right = 0 then
Send("{d down}")
\$right=1
EndIf
Sleep(1)
WEnd```

There's other code in the loop to but it didn't really slow down until i added all these if's. And taking the Sleep(1) out, resulting in 90%ish CPU usage is not an option.

##### Share on other sites

this may or may not help but you may want to try something like

```Select
Case \$variable = "value"
...do stuff here
case \$variable = "value1"
...do stuf
EndSelect```

AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!

##### Share on other sites

Your checking every instance every time, when you only need to check it once... this will speed it up a bit...

```If \$input[4]<200 and \$fire1=1 Then
MouseUp("left")
\$fire1=0
Elseif \$input[4]>200 and \$fire1=0 then
MouseDown ( "left" )
\$fire1=1
EndIf
If \$input[3]<200 and \$fire2=1 Then
MouseUp("right")
\$fire2=0
Elseif \$input[3]>200 and \$fire2=0 then
MouseDown ( "right" )
\$fire2=1
EndIf
If \$input[6]<22000 and \$forward = 1 then
Send("{w up}")
\$forward=0
ElseIf \$input[6]>22000 and \$forward = 0 then
Send("{w down}")
\$forward=1
EndIf
If \$input[6]>-22000 and \$back = 1 then
Send("{s up}")
\$back=0
ElseIf \$input[6]<-22000 and \$back = 0 then
Send("{s down}")
\$back=1
EndIf
If \$input[5]<22000 and \$left = 1 then
Send("{a up}")
\$left=0
ElseIf \$input[5]>22000 and \$left = 0 then
Send("{s down}")
\$left=1
EndIf
If \$input[5]>-22000 and \$right = 1 then
Send("{d up}")
\$right=0
ElseIf \$input[5]<-22000 and \$right = 0 then
Send("{d down}")
\$right=1
EndIf```

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

##### Share on other sites

Does anyone have any ideas for speeding this up or making it more efficient?

CODE
While 1

if \$input[4]<200 and \$fire1=1 Then

MouseUp("left")

\$fire1=0

EndIf

if \$input[4]>200 and \$fire1=0 then

MouseDown ( "left" )

\$fire1=1

EndIf

if \$input[3]<200 and \$fire2=1 Then

MouseUp("right")

\$fire2=0

EndIf

if \$input[3]>200 and \$fire2=0 then

MouseDown ( "right" )

\$fire2=1

EndIf

If \$input[6]<22000 and \$forward = 1 then

Send("{w up}")

\$forward=0

EndIf

If \$input[6]>22000 and \$forward = 0 then

Send("{w down}")

\$forward=1

EndIf

If \$input[6]>-22000 and \$back = 1 then

Send("{s up}")

\$back=0

EndIf

If \$input[6]<-22000 and \$back = 0 then

Send("{s down}")

\$back=1

EndIf

If \$input[5]<22000 and \$left = 1 then

Send("{a up}")

\$left=0

EndIf

If \$input[5]>22000 and \$left = 0 then

Send("{s down}")

\$left=1

EndIf

If \$input[5]>-22000 and \$right = 1 then

Send("{d up}")

\$right=0

EndIf

If \$input[5]<-22000 and \$right = 0 then

Send("{d down}")

\$right=1

EndIf

Sleep(1)

WEnd

There's other code in the loop to but it didn't really slow down until i added all these if's. And taking the Sleep(1) out, resulting in 90%ish CPU usage is not an option.
Every one of your If conditions gets tested, even if it is contradictory to a previous result. Make some use of 'Else' so conditions don't have to be retested and the \$input array doesn't have to be re-read. For example:

```if \$input[4] < 200 Then
if \$fire1=1  Then
MouseUp("left")
\$fire1=0
EndIf
Else
if \$fire1=0 then
MouseDown ( "left" )
\$fire1=1
EndIf
EndIf```

That example follows the else condition if \$input[4] = 200, which you didn't seem to account for, but you get the idea of the if/else...

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

##### Share on other sites

@SmOke_N: Thanks. It slightly increased the speed but not enough make me content with it.

@PsaltyDS: That method worked a little better than SmOke_N's but again, I still want it a little faster if possible.

Would use of _IsPressed instead of all the switch variables be better?

##### Share on other sites

IsPressed only tells you if the key was pressed, it doesn't do the action for you.

Edit:

You know the bigger question here is how are you creating the array that your using for these coords. That may play a bigger factor in speed than just adjusting what it is that your doing here.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

##### Share on other sites

the array is retrieved through a dll call for a joystick. no optimization can be done there. I know IsPressed is only good for detecting but that's what my switches are for: To make sure that i only send a key up when it is already down.

##### Share on other sites

Well, here's what I have right now:

```If \$input[4]<200 Then
If \$fire1=1 Then
MouseUp("left")
\$fire1=0
EndIf
Else
If \$fire1=0 then
MouseDown ( "left" )
\$fire1=1
EndIf
EndIf
If \$input[3]<200 Then
If \$fire2=1 Then
MouseUp("right")
\$fire2=0
EndIf
Else
If \$fire2=0 then
MouseDown ( "right" )
\$fire2=1
EndIf
EndIf
If \$input[6]<22000 Then
If \$forward = 1 then
Send("{w up}")
\$forward=0
EndIf
Else
If \$forward = 0 then
Send("{w down}")
\$forward=1
EndIf
EndIf
If \$input[6]>-22000 Then
If \$back = 1 then
Send("{s up}")
\$back=0
EndIf
Else
If \$back = 0 then
Send("{s down}")
\$back=1
EndIf
EndIf
If \$input[5]<22000 Then
If \$left = 1 then
Send("{a up}")
\$left=0
EndIf
Else
If \$left = 0 then
Send("{s down}")
\$left=1
EndIf
EndIf
If \$input[5]>-22000 Then
If \$right = 1 then
Send("{d up}")
\$right=0
EndIf
Else
If \$right = 0 then
Send("{d down}")
\$right=1
EndIf
EndIf```

Anymore options or is this the end of the line? I'd really like to know either way because I will need to make changes to the rest of the script based on the speed of the script.

##### Share on other sites

Well, here's what I have right now:

CODE
If \$input[4]<200 Then

If \$fire1=1 Then

MouseUp("left")

\$fire1=0

EndIf

Else

If \$fire1=0 then

MouseDown ( "left" )

\$fire1=1

EndIf

EndIf

If \$input[3]<200 Then

If \$fire2=1 Then

MouseUp("right")

\$fire2=0

EndIf

Else

If \$fire2=0 then

MouseDown ( "right" )

\$fire2=1

EndIf

EndIf

If \$input[6]<22000 Then

If \$forward = 1 then

Send("{w up}")

\$forward=0

EndIf

Else

If \$forward = 0 then

Send("{w down}")

\$forward=1

EndIf

EndIf

If \$input[6]>-22000 Then

If \$back = 1 then

Send("{s up}")

\$back=0

EndIf

Else

If \$back = 0 then

Send("{s down}")

\$back=1

EndIf

EndIf

If \$input[5]<22000 Then

If \$left = 1 then

Send("{a up}")

\$left=0

EndIf

Else

If \$left = 0 then

Send("{s down}")

\$left=1

EndIf

EndIf

If \$input[5]>-22000 Then

If \$right = 1 then

Send("{d up}")

\$right=0

EndIf

Else

If \$right = 0 then

Send("{d down}")

\$right=1

EndIf

EndIf

Anymore options or is this the end of the line? I'd really like to know either way because I will need to make changes to the rest of the script based on the speed of the script.
How about seperate compiled scripts? Split the job into two or three seperate compiled scripts all running at the same time (or as nearly so as Window's thread scheduler can handle). Each script only has to monitor and respond to one or two conditions...

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

## Create an account

Register a new account