Search the Community
Showing results for tags 'static'.
-
Hello, I wrote a benchmark script to measure variable declarations to find out whether you should focus more on static or global variables #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.5 #ce ---------------------------------------------------------------------------- #Region Pre-Setting Local $iTally1 = 0 Local $iTally2 = 0 Local $iTally3 = 0 Local $iTally4 = 0 Local $iTally5 = 0 Local $iTally6 = 0 Local $iTally7 = 0 Global $GLOBALCONST1 = 1 Global $GLOBALCONST2 = 1 Global $GLOBALCONST3 = 1 Global $GLOBALCONST4 = 1 Global $GLOBALCONST5 = 1 #EndRegion Pre-Setting #Region Test Functions Func s1() Static $i = $GLOBALCONST1 Return $i EndFunc Func g2() Return $GLOBALCONST2 EndFunc Func g3() Static $i7 = "gsdgdfegbgbrwefw" Return $GLOBALCONST3 EndFunc Func g4() Static $i1 = 1 Static $i2 = "asd" Static $i3 = 234 Static $i4 = True Static $i5 = [0] Static $i6 = "hgsdg" Static $i7 = 1 Static $i8 = 1 Static $i9 = 1 Static $i0 = 1 Return $GLOBALCONST4 EndFunc Func g5() Local $i = $GLOBALCONST5 Return $i EndFunc Func g6() Local $i = 1 Return $i EndFunc Func g7() Return 1 EndFunc #EndRegion Test Functions #Region Benchmark Loop For $i = 0 To 15 Local $tDelta = TimerInit() Do $iTally1 += s1() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally2 += g2() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally3 += g3() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally4 += g4() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally5 += g5() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally6 += g6() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally7 += g7() Until TimerDiff($tDelta) >= 1000 Next #EndRegion Benchmark Loop ConsoleWrite(@CRLF&"Static1: "&$iTally1&" pkt"&@CRLF&"Global2: "&$iTally2&" pkt"&@CRLF&"Global3: "&$iTally3&" pkt"&@CRLF&"Global4: "&$iTally4&" pkt"&@CRLF&"Local5: "&$iTally5&" pkt"&@CRLF&"Local6: "&$iTally6&" pkt"&@CRLF&"Hardcode7:"&$iTally7&" pkt"&@CRLF) #cs Result Static1: 10291881 pkt global to static Global2: 13977324 pkt only global Global3: 9886169 pkt global and static Global4: 2933051 pkt global and many statics Local5: 9937314 pkt global to local Local6: 10306484 pkt only local Hardcode7: 14835319 pkt no variable #ce Result: 100% no variable, hardcore value 94% only global variable use 69% only local variable use with hardcore value set 69% only static variable use with global variable value set 67% declaration of local variable with global variable value set 66% only global variable use with one static variable beside 20% only global variable use with ten static variables beside My thesis of the result: Be careful with declarations, whether local, global or static Note: in my test the global variable performance was better than the local one, but in practice the global one would lose performance due to multiple operations What is your best practice sharing data between multiple functions?
- 1 reply
-
- performance
- declaring
-
(and 3 more)
Tagged with:
-
This is the error message I got: "Cannot make existing variables static." I'm trying to use static variable as a means to iterate a section of my script. Original Edit1 while 1 global $initial, $constant = 1 $x = $initial + ($constant * SomeFunc()) msgbox(0, '', $x) wend func SomeFunc() global static $counter = -1 $counter += 1 return $counter endfunc
-
An example (or one variation at least) that I created in a discussion we (devs/mods) were having about protecting global variables. ; An enumeration for the getters and setters, as I hate having to write zero and one Global Enum $VERSION_WRAPPER_GET, $VERSION_WRAPPER_SET ; Unused, as I have hidden behind functions, so users who might feel the need to do this $g_iVersion = 'Haha, I''m no a number', can't! ; Global $g_iVersion = 0 ; Basically this is encapsulation and is just one of many principals found in Object Oriented Programming (OOP) ; NOTE: There is no way for me to access the "global" version variable without using the _Get/Set_Version() function(s) Example() Func Example() ; Set to 10, as only integer datatypes are allowed _Set_Version(10) ; Displays 10 ConsoleWrite('1. ' & _Get_Version() & @CRLF) ; Set to a non-digit. Such a bad user/dev! _Set_Version('Not a number') ; Displays 10, as a string was passed previously that wasn't valid ConsoleWrite('2. ' & _Get_Version() & @CRLF) ; Call another function to show that it's not just visible within this function Another_Func() ; Displays 99, which was previously set in the Another_Func() ConsoleWrite('4. ' & _Get_Version() & @CRLF) ; If the variable located in __Version_Wrapper() was purely a global variable, then anything could be assigned to it ; without proper checking. As you can see below, the variable is only assigned to when certain criteria is met ; Before someone mentions performance, you have to weigh up the pros and cons of such a design choice. If you really need data to be protected ; from your users mingling with it, then this is one of many correct approaches to encapsulate data. Otherwise if you're not concerened, then ; maybe it's not for you. You're the developer when it comes down to it EndFunc ;==>Example Func Another_Func() ; Displays 10 ConsoleWrite('3. ' & _Get_Version() & @CRLF) ; Set to 99 and return back to the previous function i.e. Example() _Set_Version(99) EndFunc ;==>Another_Func ; Getter Func _Get_Version() Return __Version_Wrapper($VERSION_WRAPPER_GET) EndFunc ;==>_Get_Version ; Setter Func _Set_Version($iVersion) Return __Version_Wrapper($VERSION_WRAPPER_SET, $iVersion) EndFunc ;==>_Set_Version ; This is a wrapper for the local static variable that will be visible only in the function and for the duration of the application's ; lifecycle ; $iType: Either $VERSION_WRAPPER_GET or $VERSION_WRAPPER_SET ; $iVersion: Used when the type is $VERSION_WRAPPER_SET Func __Version_Wrapper($iType, $iVersion = Null) ; Create a local static variable and initialise to 0 Local Static $s_iVersion = 0 Switch $iType Case $VERSION_WRAPPER_GET ; Getter Return $s_iVersion Case $VERSION_WRAPPER_SET ; Setter ; Now is the chance to check if the value meets certain criteria e.g. [0-9]+ and not a string datatype If StringRegExp($iVersion, '^\d+$') And Not IsString($iVersion) Then $s_iVersion = $iVersion EndIf EndSwitch ; Return null by default, especially if the type is as a setter Return Null EndFunc ;==>__Version_Wrapper
- 7 replies
-
- encapsulation
- getters
-
(and 3 more)
Tagged with:
-
Hi everyone, Using this script (it has a broken link but here are the files) I'm trying to record a short clip 1 second or shorter longer (although you can assume it's always 1 second) and every time I record the clip (mp3 however it's the same on wav) it ends up with an extremely short static sound at the start of each clip even with the highest quality sound. Is there anyway you can trim the file down, also using bass, for example taking the first 100 miliseconds off the start (it has to be mp3 though)? Or on the other hand, does anyone know of another bass recording tool that allows that diversity of options, e.g quality (not essential but preferable), volume and microphone choices (these two are important) which doesn't have this issue? Thanks Apples292
-
Playing around a bit with this idea. Note: Needs at least AutoIt version 3.3.9.0 (#1257: Fix bugs in Static array handling) Func My_Statics($sName, $vData = '') ;; parameter checkups If 1 Then ;; ... If Not IsString($sName) Then Return SetError(2) ;; name -> string only. If Not $sName Then Return SetError(3) ;; name -> no empty string. ;If StringInStr($sName, ' ') then Return SetError(4) ;; block spaces in names. ... etc ;; not checking for additional odd name cases. (presuming general valid variable name cases.) ;; note: name string are not case-sensitive in this case. EndIf Local Enum $_ind_ ;; array index field. (make global when warranted.) Local Static $asNames[1] = [0] Local Static $avData[1] = [0] Local $iSize = $asNames[$_ind_] Local $out = '' Local $err = 0 If @NumParams > 1 Then ;; data input mode. Do ;; used as name-found bailout point. For $i = 1 To $iSize If $asNames[$i] = $sName Then $avData[$i] = $vData ExitLoop 2 EndIf Next ;; Name not found. Add new name entry. $iSize += 1 ReDim $asNames[1 + $iSize] ReDim $avData[1 + $iSize] ; $asNames[$_ind_] = $iSize $avData[$_ind_] = $iSize ; $asNames[$iSize] = $sName $avData[$iSize] = $vData Until 1 Else ;; recall $err = 1 For $i = 1 To $asNames[$_ind_] If $asNames[$i] = $sName Then $out = $avData[$i] $err = 0 ExitLoop EndIf Next EndIf Return SetError($err, 0, $out) EndFunc (Only tested with strings and a array. But seems ok so far.) Error 1 (+empty string): Name not found, recall mode only. Error 2+3: Name parameter hickup. - Fixed missing constant.