Sign in to follow this  
Followers 0
Shafayat

Speed test between _ArrayAdd, Scripting Dictionary, Array command (inbuilt)

19 posts in this topic

#1 ·  Posted (edited)

Recently I've been working with Scripting Dictionary Object a lot. So, I did a basic test and got just the result I had expected.

#cs
Speed test between -
1) _ARRAYADD
2) Scripting Dictionary Add
3) Array command (inbuilt)
#ce

#include <Timers.au3>
#include <Array.au3>

Global $count = 100000
;~ Global $count = 1000000 ; Use if you can let your computer have fun for a few minutes
Global $rnd = Random(0, 9, 1) & Random(0, 9, 1) & Random(0, 9, 1) & Random(0, 9, 1) & Random(0, 9, 1)

; ==================== TEST ARRAYADD ==================== ;
$starttime = _Timer_Init()
Global $testarray
For $i = 1 To $count
    _ArrayAdd($testarray, $rnd)
Next
$TEST_ARRAYADD = _Timer_Diff($starttime)
; ======================================================= ;

; =================== TEST DICTIONARY =================== ;
$starttime = _Timer_Init()
Global $testdict = ObjCreate("Scripting.Dictionary")
For $i = 1 To $count
    $testdict($rnd) = $rnd
Next
$TEST_DIC = _Timer_Diff($starttime)
; ======================================================= ;

; ================== TEST ARRAY GENERAL ================== ;
$starttime = _Timer_Init()
Global $testarray2[$count + 1]
For $i = 1 To $count
    ;_ArrayAdd($testarray, $rnd)
    $testarray2[$i] = $rnd
Next
$TEST_ARRAY = _Timer_Diff($starttime)
; ======================================================= ;

MsgBox(0, "Speed Test Results:", "With _ArrayAdd = " & $TEST_ARRAYADD & @CRLF & "With ScriptingDictionary = " & $TEST_DIC & @CRLF & "With Simple Array = " & $TEST_ARRAY)

post-23819-12688078631195_thumb.png

Edited by Shafayat

[Not using this account any more. Using "iShafayet" instead]

Share this post


Link to post
Share on other sites



I added the struct to your example:

; ================== TEST STRUCT ======================== ;
Global $tStruct = DllStructCreate("uint[" & $count & "]")
$starttime = _Timer_Init()
For $i = 1 To $count
    DllStructSetData($tStruct, $i, $rnd)
Next
$TEST_STRUCT = _Timer_Diff($starttime)
$tStruct = ""
; ======================================================= ;


MsgBox(0, "Speed Test Results:", "With _ArrayAdd = " & $TEST_ARRAYADD & @CRLF & _
                                 "With ScriptingDictionary = " & $TEST_DIC & @CRLF & _
                                 "With Simple Array = " & $TEST_ARRAY & @CRLF & _
                                 "With Struct = " & $TEST_STRUCT)

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

_ArrayAdd is slow because it's redims the array every call, your comparision is wrong here.

We can change the function a little bit and add an Element parameter:

;#include <Array.au3>

Global $count = 10000
Global $testarray[$count + 1]

; ==================== TEST ARRAYADD ==================== ;
$starttime = TimerInit()
For $i = 1 To $count
    _ArrayAdd($testarray, $i, $i)
Next
$TEST_ARRAYADD = TimerDiff($starttime)
; ======================================================= ;

; ================== TEST ARRAY GENERAL ================== ;
$starttime = TimerInit()
For $i = 1 To $count
    $testarray[$i] = $i
Next
$TEST_ARRAY = TimerDiff($starttime)
; ======================================================= ;

MsgBox(0, "Speed Test Results:", "With _ArrayAdd = " & $TEST_ARRAYADD & @CRLF & "With Simple Array = " & $TEST_ARRAY)

Func _ArrayAdd(ByRef $avArray, $vValue, $iElement=-1)
    If Not IsArray($avArray) Then Return SetError(1, 0, -1)
    If UBound($avArray, 0) <> 1 Then Return SetError(2, 0, -1)
    
    Local $iUBound = UBound($avArray)
    
    If $iElement > 0 And $iElement < $iUBound Then
        $iUBound = $iElement
    Else
        ReDim $avArray[$iUBound + 1]
    EndIf
    
    $avArray[$iUBound] = $vValue
    Return $iUBound
EndFunc

but that is still slower than custom assignment.


Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

So...what is the benifit of using _ArrayAdd() again (or any "more complex" method, for that matter)? from this, it looks like the most simple and straight-forward method beats the others by a long shot. The Scripting Dictionary object is an interesting take on filling an array, though.


Share this post


Link to post
Share on other sites

I knew why _arrayadd is slower. But I don't see any way to fix it either. It will always be slower, yet still easy to use.

Actually I did the test to find out if I use Scripting Dictionary object instead of array functions, what kind of loss I receive in speed. The answer I get is "not much" or "acceptable". I see that this object provides many of the flexibilities of _array functions with less compromising on the speed. So, I'm most likely going to use it in my later scripts.


[Not using this account any more. Using "iShafayet" instead]

Share this post


Link to post
Share on other sites

I don't see any way to fix it either

I personaly use global declaration of the array with specific size, and i redim it only when there is need to.

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Try this one. Also, fixed your use of the dictionary. Best speed so far for a dynamically filled array.

#NoTrayIcon

#cs
Speed test between -
1) _ARRAYADD
2) Scripting Dictionary Add
3) Array command (inbuilt)
#ce

#include <Timers.au3>
#include <Array.au3>

Global $count = 100000
;~ Global $count = 1000000 ; Use if you can let your computer have fun for a few minutes
Global $rnd = Random(0, 9, 1) & Random(0, 9, 1) & Random(0, 9, 1) & Random(0, 9, 1) & Random(0, 9, 1)

; ==================== TEST ARRAYADD ==================== ;
$starttime = _Timer_Init()
Global $testarray
For $i = 1 To $count
    _ArrayAdd($testarray, $rnd)
Next
$TEST_ARRAYADD = _Timer_Diff($starttime)
; ======================================================= ;

; =================== TEST DICTIONARY =================== ;
$starttime = _Timer_Init()
Global $testdict = ObjCreate("Scripting.Dictionary")
For $i = 1 To $count
    $testdict.Item($i) = $rnd
    ;$testdict.Add($i, $rnd)
Next
$TEST_DIC = _Timer_Diff($starttime)
; ======================================================= ;

; ================== TEST ARRAY GENERAL ================== ;
$starttime = _Timer_Init()
Global $testarray[1] = [0]
For $i = 1 To $count
    $testarray[0] += 1
    If UBound($testarray) <= $testarray[0] Then ReDim $testarray[UBound($testarray) * 2]
    $testarray[$testarray[0]] = $rnd
Next
ReDim $testarray[$testarray[0] + 1]
$TEST_ARRAY = _Timer_Diff($starttime)
; ======================================================= ;

MsgBox(0, "Speed Test Results:", "With _ArrayAdd = " & $TEST_ARRAYADD & @CRLF & "With ScriptingDictionary = " & $TEST_DIC & @CRLF & "With Dynamic Array = " & $TEST_ARRAY)
Edited by wraithdu

Share this post


Link to post
Share on other sites

_ArrayAdd() does return @error with using a non-array variable. Perhaps checking @error may help to validate the test.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

I've been using Scripting.Dictionary since it's accessible from many programming languages. I'm curious if anyone has done any capacity benchmarks? The maximum size may vary with physical ram but I'm wondering if there's some limitations in the implementation such as maximum number of entries or some other.

As an example I'm using it to store text comments associated with files. The comment shows in a ToolTip when you arrow key in Explorer. The file path is the key. But since entry is HotKey to an InputBox, enter text, repeat, I'm not really up for entering 4000 comments to see what it can handle.

Seems to be fine for casual use. I'm just curious if anyone has run into some number where it bogs badly or just craps out altogether.

Edited by MilesAhead

Share this post


Link to post
Share on other sites

Try this one. Also, fixed your use of the dictionary. Best speed so far for a dynamically filled array.

Added static array test - of course speed winner:

; ================== TEST ARRAY STATIC ================== ;
$starttime = _Timer_Init()
Global $testarray[$count]
For $i = 0 To $count - 1
    $testarray[$i] = $rnd
Next
$TEST_ARRAY_static = _Timer_Diff($starttime)
; ======================================================= ;

Share this post


Link to post
Share on other sites

Try this one. Also, fixed your use of the dictionary. Best speed so far for a dynamically filled array.

#NoTrayIcon
 
#cs
Speed test between -
1) _ARRAYADD
2) Scripting Dictionary Add
3) Array command (inbuilt)
#ce
 
#include <Timers.au3>
#include <Array.au3>
 
Global $count = 100000
;~ Global $count = 1000000 ; Use if you can let your computer have fun for a few minutes
Global $rnd = Random(0, 9, 1) & Random(0, 9, 1) & Random(0, 9, 1) & Random(0, 9, 1) & Random(0, 9, 1)
 
; ==================== TEST ARRAYADD ==================== ;
$starttime = _Timer_Init()
Global $testarray
For $i = 1 To $count
    _ArrayAdd($testarray, $rnd)
Next
$TEST_ARRAYADD = _Timer_Diff($starttime)
; ======================================================= ;

Not sure If I am missing something, please excuse me if I am,

but _ArrayAdd is a million times slower than this code suggests,

as if you declare it like you did, this array is neither created nor redimmed by _ArrayAdd,

so effectively, all youre timing there, is Autoit's Error response speed from the func.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

@Zip Read the posts form above. the speed problem is because _ArrayAdd() ReDim the Array every time you called, that takes most time.. that is what i undertand reading this topic. Edited by monoscout999

Share this post


Link to post
Share on other sites

Yes, but that will not happen, with the code I quoted ... all it times, is the calls to & error returns from _ArrayAdd.

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

This, I think, should show the real speed difference, using the examples above combined,

(I left the Dynamic array over estimation of redimming to *2, if used with +1, it will be ~ as slow as _arrayadd)

I changed it to use a 1000 values, as 100,000 values is too slow if _ArrayAdd truly does its thing.

#NoTrayIcon
#include <Timers.au3>
#include <Array.au3>
#cs
Speed test between -
1) _ARRAYADD
2) Scripting Dictionary Add
3) Array command (inbuilt)
#ce
Global $count = 1000
Global $rnd = Random(0, 9, 1) & Random(0, 9, 1) & Random(0, 9, 1) & Random(0, 9, 1) & Random(0, 9, 1)
ConsoleWrite('-('&@ScriptLineNumber&') :['&@error&'|'&@Extended&'] $rnd = '&$rnd&@lf) ;### Debug Console
; ==================== TEST ARRAYADD ==================== ;
ConsoleWrite(@lf & '>  ==================== TEST ARRAYADD ==================== ' & @LF)
$starttime = _Timer_Init()
Dim $testarray[1]=[$count]
For $i = 1 To $count
    $ArrayAdd=_ArrayAdd($testarray, $rnd)
Next
$TEST_ARRAYADD = _Timer_Diff($starttime)
ConsoleWrite(':'&@ScriptLineNumber&': $TEST_ARRAYADD = '&$TEST_ARRAYADD&@lf) ;### Debug Console
; ======================================================= ;
; =================== TEST DICTIONARY =================== ;
ConsoleWrite(@lf & '>  =================== TEST DICTIONARY =================== ' & @LF)
$starttime = _Timer_Init()
Global $testdict = ObjCreate("Scripting.Dictionary")
For $i = 1 To $count
    $testdict.Item($i) = $rnd
Next
$TEST_DIC = _Timer_Diff($starttime)
ConsoleWrite(':'&@ScriptLineNumber&': $TEST_DIC =  '&$TEST_DIC&@lf) ;### Debug Console
; ======================================================= ;
; ================== TEST ARRAY DYNAMIC ================== ;
ConsoleWrite(@lf & '>  ================== TEST ARRAY DYNAMIC ================== ' & @LF)
$starttime = _Timer_Init()
Global $testarray[1] = [0]
For $i = 1 To $count
    $testarray[0] += 1
    If UBound($testarray) <= $testarray[0] Then ReDim $testarray[UBound($testarray) * 2]
    $testarray[$testarray[0]] = $rnd
Next
ReDim $testarray[$testarray[0] + 1]
$TEST_ARRAY = _Timer_Diff($starttime)
ConsoleWrite(':'&@ScriptLineNumber&': $TEST_ARRAY  = '&$TEST_ARRAY &@lf) ;### Debug Console
; ======================================================= ;
; ================== TEST ARRAY STATIC ================== ;
ConsoleWrite(@lf & '>  ================== TEST ARRAY STATIC ================== ' & @LF)
$starttime = _Timer_Init()
Global $testarray[$count]
For $i = 0 To $count - 1
    $testarray[$i] = $rnd
Next
$TEST_ARRAY_static = _Timer_Diff($starttime)
ConsoleWrite(':'&@ScriptLineNumber&': $TEST_ARRAY_static = '&$TEST_ARRAY_static&@lf) ;### Debug Console
; ======================================================= ;
MsgBox(0, "Speed Test Results:", "With _ArrayAdd = " & $TEST_ARRAYADD & @CRLF & "With ScriptingDictionary = " & $TEST_DIC & @CRLF & "With Dynamic Array = " & $TEST_ARRAY & @CRLF & "With Static Array = " & $TEST_ARRAY_static)
Edited by Zip

Share this post


Link to post
Share on other sites

I don´t get your "Dynamic Array" method...why * 2 it should be + 1 to be fair...

Share this post


Link to post
Share on other sites

well, because its useless without it ..

using *2 makes it a valid way to do it speed wise, change it to +1, & your'e back to _ArrayAdd speed.

Share this post


Link to post
Share on other sites

I see now! good method i don`t know where to apply, because if you know how many data you will add you use the "Static array" method

Share this post


Link to post
Share on other sites

Yes, but that will not happen, with the code I quoted ... all it times, is the calls to & error returns from _ArrayAdd.

Yeah, there's an error in the test code. For the _ArrayAdd test, the $testarray var must be declared an array, Global $testarray[1].

Share this post


Link to post
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
Sign in to follow this  
Followers 0