Jump to content

ArraysVsVars


Recommended Posts

prolly a stupid question. :) but i've googled it, even with the prefix to search only autoitscript.com, i've gotten mixed results, and nothing that is definitive.

is accessing a 1d array element repeatedly, slower than having simple vars?(i only use an array for looping, but i could easily change it to simple variables if it faster.)

Link to comment
Share on other sites

hi,

Very interesting question, I hatched up this little test script and it seems legitimize your question ;)

Local $aiTestArray[1] = [0]
Local $iVar = 0
Local $iTimeStamp, $iResult, $iOperations = 10000000

ConsoleWrite("Initiating test with " & $iOperations & " read & writes on 1D Array. ")
$iTimeStamp = TimerInit()
For $i = 1 To $iOperations
    $aiTestArray[0] += 1
Next
$iResult = TimerDiff($iTimeStamp)
ConsoleWrite("Time taken: " & $iResult & "ms" & @CRLF)

ConsoleWrite("Initiating test with " & $iOperations & " read & writes on simple variable. ")
$iTimeStamp = TimerInit()
For $i = 1 To $iOperations
    $iVar += 1
Next
$iResult = TimerDiff($iTimeStamp)
ConsoleWrite("Time taken: " & $iResult & "ms" & @CRLF)

I got:

Initiating test with 10000000 read & writes on 1D Array. Time taken: 19514.9450756538ms
Initiating test with 10000000 read & writes on simple variable. Time taken: 11091.0376350476ms

:)

-smartee

Link to comment
Share on other sites

so a variable is more efficient, when looping is not necessarily needed...? (yes it is only a small amount of time, but with enough variables/array elements it might become significant.)

Link to comment
Share on other sites

The benchmark results indicates using a simple variable is quicker, in the test with 10 million read & writes, operations using the simple variable concluded ~8 seconds earlier than those using an element of the 1D array.

So, yes, I guess using a simple variable is more efficient. (whether or not you're looping) :)

-smartee

Link to comment
Share on other sites

And that makes sense for most programming languages, since with an array, you've got one more layer of indirection present. For example to get the value of a variable, the program must find the memory address of the variable and then extract the value. For an array, the program must find the memory address of the array, and then find the address of the item desired based on the array index before extracting the value.

Link to comment
Share on other sites

It doesn't seem like the difference should be that great. Isn't accessing the first element of an array the same as accessing a lone variable. Because isn't an array merely a set of variables placed in contiguous memory?

Link to comment
Share on other sites

Contiguous memory? In C but not in AutoIt!

A C array is

<sometype> array[bound];

Every element is an instance of <sometype>, that is "has a fixed size: sizeof(<sometype>)

Accessing array[42] is accessing &array + 42*sizeof(<sometype>)

Bounds are not checked (in plain C), so accessing array[-3] or array[bound + 1] generally causes an exception and program crash.

An AutoIt array is an array of AutoIt Variants which may hold any type in each element, even if the documentation recommends not doing it.

As a consequence, an AutoIt array must consist of a C-style array of pointers, each pointing to a Variant instance or to another array of pointers in the case of a multi-dimensional array.

The interpreter has to check bounds and incure at least one more level of indirection.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

I thought I would try this using DllStruct, admittedly I thought it would be on par with vars.

I was wrong.

So much so, that I had to lower $iOperations before It was Wednesday.

Local $aiTestArray[1] = [0]
Local $DllStruct = DllStructCreate("int")
If @error Then Exit
DllStructSetData($DllStruct,1,0)
If @error Then Exit
Local $iVar = 0
Local $iTimeStamp, $iResult, $iOperations = 10000

ConsoleWrite("Initiating test with " & $iOperations & " read & writes on 1D Array. ")
$iTimeStamp = TimerInit()
For $i = 1 To $iOperations
    $aiTestArray[0] += 1
Next
$iResult = TimerDiff($iTimeStamp)
ConsoleWrite("Time taken: " & $iResult & "ms" & @CRLF)

ConsoleWrite("Initiating test with " & $iOperations & " read & writes on simple variable. ")
$iTimeStamp = TimerInit()
For $i = 1 To $iOperations
    $iVar += 1
Next
$iResult = TimerDiff($iTimeStamp)
ConsoleWrite("Time taken: " & $iResult & "ms" & @CRLF)


ConsoleWrite("Initiating test with " & $iOperations & " read & writes on DllStruct. ")
$iTimeStamp = TimerInit()
For $i = 1 To $iOperations
    DllStructGetData($DllStruct,1)
    DllStructSetData($DllStruct,1,$i)
Next
$iResult = TimerDiff($iTimeStamp)
ConsoleWrite("Time taken: " & $iResult & "ms" & @CRLF)

Unless I'm doing it wrong.

Initiating test with 10000 read & writes on 1D Array. Time taken: 4.73314913616375ms

Initiating test with 10000 read & writes on simple variable. Time taken: 2.67970522562907ms

Initiating test with 10000 read & writes on DllStruct. Time taken: 32.9240830606678ms

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Of course DllStructs are much, much worst than even n-D arrays, just as you test shows.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

so far...

1st PLACE) Variant

2nd Place) 1d array

3rd place) DllStruct

Link to comment
Share on other sites

The another fight of the night is, passing variables vs values

$a = 2
$b = 4
$c = 5
$d = 73
$e = 16
$var = 0
$begin = TimerInit()
For $i = 0 to 100000
    $var = $i+$a*$b-$c*$d/$e
Next
$diff1 = Timerdiff($begin)
$begin = TimerInit()
For $i = 0 to 100000
    $var = $i+2*4-5*73/16
Next
$diff2 = Timerdiff($begin)
consolewrite(@CRLF&"time operating variables 100000 = "&$diff1&" ms."&@CRLF)
consolewrite(@CRLF&"time operating integers 100000 = "&$diff2&" ms."&@CRLF)

Maybe i cheat this one jiji. there are better way to do this and where the variables are faster.

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