# Coding Efficiency - General Question

## Recommended Posts

Ok, This question may seam stupid to many people and in some ways it kinda is. If someone has a powerful computer or the amout of CPU difference are so small it doesn't matter but i belive anyone can write some code to do something, the real challanage / fun is finding the best / most effiect way of doing the same thing.

So here is the question. Out of these 3 examples, which would the forum say is the best standards in terms of coding style or performance

;Designed for a 1366 x 768 Display

;Code Detects Mouse Location on Screen and Reactions when its near the borders

Example A

;I belive though this looks the best, and takes a bit of understanding to follow is the lest effecitive as it calls a function (abs), has mathematical equation involved (- a number) and the > statement that all examples have.

```while 1
sleep(250)

\$A = mousegetpos()

If abs(\$A[1] - 383) > 381 Then
ConsoleWrite("A")
Elseif abs(\$A[0] - 683) > 681 Then
ConsoleWrite("B")
EndIf

Wend```

Example B

The basic if layout, easy to understand but gets confusing fast if there are tones or many ifs nested. Select/case is the same, uses more lines but can be easier to follow. As its just loading number from array and then one > is very effective?

```while 1
sleep(250)

\$A = mousegetpos()

If \$A[1] < 2 Then
ConsoleWrite("A")
Elseif \$A[1] > 666 Then
ConsoleWrite("A")
Elseif \$A[0] < 2 Then
ConsoleWrite("B")
Elseif \$A[0] > 1364 Then
ConsoleWrite("B")
EndIf```

Example C

Same as the code above, but uses less lines. Its still just 4 If statements.

```While 1
sleep(250)

\$A = mousegetpos()

If \$A[1] < 2 or \$A[1] > 666 Then
ConsoleWrite("A")
Elseif \$A[0] < 2 or \$A[0] > 1364 Then
ConsoleWrite("B")
EndIf

WEnd```

P.S No comments about programming in Binary or Machine code for maximum Efficiency ^^ I enjoy programming in autoit and using autoit syntax, just looking to find which might be the best in this example "which I would then learn for future examples".

Edited by IanN1990

##### Share on other sites

Hi,

You can talk about efficiency when you, as a human, you can see the difference in speed.

You are asking : What's the best way to write a program ?

I have recently learned that you have to separate the input of the processing of the output as much as you can.

But it's an algorithmic question, and here there is only few lines of code, so it's insignificant.

I would take the example C, which would be instinctive.

Hope this helps ! I'm only a beginner ^^

Br, 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 on other sites

All the examples contain hard-coded numbers in the source, which is generally frowned upon.

I'd shoot for something more flexible, less prone to requiring maintenance.

This ought to work for any resolution, with nothing hard-coded.

I set the border threshhold to 2 pixels in this example:

```#include <Misc.au3> ; needed for _IsPressed()

Global \$iHorz_Center = @DesktopWidth / 2 - .5, \$iHorz_Offset, \$sHorz_Alert
Global \$iVert_Center = @DesktopHeight / 2 - .5, \$iVert_Offset, \$sVert_Alert
Global \$aAlert_Status[2] = ["","ALERT"], \$iThreshhold = 2 ; pixels

While 1
If _IsPressed("1B") Then ExitLoop ; ESC key to exit program
\$aPos = MouseGetPos()
\$iHorz_Offset = Abs(Abs(\$aPos[0] - \$iHorz_Center) - \$iHorz_Center) ; distance to H border
\$iVert_Offset = Abs(Abs(\$aPos[1] - \$iVert_Center) - \$iVert_Center) ; distance to V border
; tooltip shows center location, current distance from border, and alert status
ToolTip("H: " & \$iHorz_Center & " " & \$iHorz_Offset & " " & \$sHorz_Alert & @CRLF & "V: " & \$iVert_Center & " " & \$iVert_Offset & " " & \$sVert_Alert, \$iHorz_Center, \$iVert_Center)
sleep(100)
WEnd```

typo

Edited by Spiff59

##### Share on other sites

To me the examples are variations in coding style, and not about efficiency. I mean if I'd write this code I'd start with example B, then rewrite this into ex. C and maybe take it to ex. A but I wouldn't consider one more efficient than the other as there is hardly any computation taking place. The differences in performance are insignificant in this case.

It starts to become fun when the math involved becomes more complex and there is more than one solution to get results: What is the most efficient algorithm to determine if a number is a prime?

[center]Spiderskank Spiderskank[/center]GetOpt Parse command line options UDF | AU3Text Program internationalization UDF | Identicon visual hash UDF

##### Share on other sites

@Spiff59. I have thought about changing my coding to something more flexible but generally has shown by your code being more flexible does lead to a performance loss. Example would be, every time the code is run it needs to get the desktop values, do the maths, and then call a variable. Rather then the variable being pre-calculated and hardcoded. As the script is designed for 3 screens, and runs odd functions "like opening windows media player / kmplayer / firefox". Its not something i dont think many people would be intereseted in. As no plan for release, makes sense in my case to hardcode it. But i do see where your coming from

@Firefox As a Person, I can see visual no difference in speed, performance, run time or exit time between each of the examples. As Dany says in Post #4 its most about coding styles. I still like the idea of getting some that looks proffesional and more importantly is as effective / efficent as posible (I do also understand there is a fine line of having something too efficient it loses structure or becomes worse in the long run "so lets say IF statements are the effective, so having millions even if that makes the coding a nightmare to understand, but then where I stop ? I should just program in machine code etc etc.").

In my head what it comes down to is something like this.

Every 250 seconds

In example A.

Checks Array - 1 Function

Does Maths - 2 Function

Runs ABS - 3 Function

Checks > 383 - 4 Functions

All in First IF, this is repeated for second if.

As every 250 mini seconds. This has to do 8 things.

In the second example.

Check Array - 1 Function

Check < 2 - 2 Function

x4 = 8 Things

Third Example is same as second.

So Maybe dany is right, and at such a simple level all of this is trival. (Well, it's trivial, as even a terrible computer wouldn't struggle with something like this but as i said in first post. When you have thosuands of lines of codes, sometimes the little things can build up). Personally, i think example A looks the best and most proffesional, but because its calling a function "abs" and doing maths "-x" then it has to be less effective then just saying "is x bigger then x" but then the others are having to "check info" from a array more often...ahhhhhhh

##### Share on other sites

Write code for people first and computers second.

The bottleneck in your code will never be where you expect it. There is a profiler on these forums somewhere. I'm sure a good search will find it.

##### Share on other sites

there is hardly any computation taking place

at such a simple level all of this is trival

I'd have to agree. There's nothing in there except calls to MouseGetPos() to worry about.

The rest, simple math and functions like Abs() (that simply zeroes out the sign bit) aren't going to add enough clock cycles to your execution time to be of any concern.

##### Share on other sites

I mean if I'd write this code I'd start with example B, then rewrite this into ex. C and maybe take it to ex. A

Same.

but then where I stop ?

Where it's understandable, because it's important when you have to take up something you've done few times ago or to BE understood (in your coding way).

Br, 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 on other sites

Write code for people first and computers second.

I agree. There's far more efficiency to be won by clear code and commenting things rather than eliminating lines of code by grouping If's or whatever.

Personally, i think example A looks the best and most proffesional...

Meh, it requires the most thought when first encountered (wtf is happening here?) but that isn't a criterium for professional looking code. The whole notion of 'professional looking' code doesn't fly imho. Code can look professional if it adheres to some coding standards and naming conventions, but still be horribly inefficient in what it's doing.

I can understand what you're talking about and it's of a different category than optimizing mathematical computations I mentioned in my first post. The efficiency of the interpreter also matters here.

Consider deeply nested arrays:

```Global \$i, \$sTest, \$aTest[1][1][1][1] = [[[['test']]]]
Global \$iTime, \$iTime2, \$iStart = TimerInit()
While \$i < 100000
If 'test' = \$aTest[0][0][0][0] Then
\$i += 1
EndIf
WEnd
\$iTime = TimerDiff(\$iStart) / 1000
\$iStart = TimerInit()
\$sTest = \$aTest[0][0][0][0]
\$i = 0
While \$i < 100000
If 'test' = \$sTest Then
\$i += 1
EndIf
WEnd
\$iTime2 = TimerDiff(\$iStart) / 1000
MsgBox(0, '', \$iTime & @CRLF & \$iTime2)```

Now, at first sight you might say that the first loop looks best and most efficient (or professional), because less variables = less memory etc. but the opposite is true. The second loop is much, much faster than the first. There's still very little computation involved but now the interpreter begins to play an important role.

In C it doesn't really matter what code you pick as any (decent) preprocessor optimizes this anyway. But in interpreted languages you'll have to do this yourself a bit. And in case of doubt, simply write a simple test case and try to find out what's best.

edit: Here's another nice and familiar example:

```Global \$i, \$aTest[100000]
Global \$iTime, \$iTime2, \$iStart = TimerInit()
While \$i < UBound(\$aTest)
\$i += 1
WEnd
\$iTime = TimerDiff(\$iStart) / 1000
\$iStart = TimerInit()
\$i = 0
While \$i < 100000
\$i += 1
WEnd
\$iTime2 = TimerDiff(\$iStart) / 1000
MsgBox(0, '', \$iTime & @CRLF & \$iTime2)```
Edited by dany

[center]Spiderskank Spiderskank[/center]GetOpt Parse command line options UDF | AU3Text Program internationalization UDF | Identicon visual hash UDF

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

×

• Wiki

• Back

• Git

## Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.