Jump to content

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


Shafayat
 Share

Recommended Posts

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]

Link to comment
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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

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

Link to comment
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.

 

Spoiler

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

Spoiler

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

Link to comment
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.

Link to comment
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]

Link to comment
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.

 

Spoiler

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

Spoiler

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

Link to comment
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)
; ======================================================= ;

; =================== 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
Link to comment
Share on other sites

  • 1 year later...

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
Link to comment
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)
; ======================================================= ;
Link to comment
Share on other sites

  • 2 weeks later...

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.

Link to comment
Share on other sites

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
Link to comment
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].
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...