Sign in to follow this  
Followers 0
Pured

Variable verses Array Difference?

12 posts in this topic

Hello,

I just wanted to know why variables don't need to be explicitly defined as "global/local" and arrays do. I've read that arrays need it, but not why.

 

Thank you in advance.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I thinking becouse is diferent data types, one is 'array', other is the rest (string, int, etc).

I can't anymore write a code whitout local or global.

And I always use:

Opt("MustDeclareVars", 1)

It force declare all variables with local or global.

Define a variable with DIM, it's old. I do not use.

If is a small code, no problem.

If the code have more than 100 lines... have so much variables, I prefer declare.

 

Edited by Luigi

Visit my repository

Share this post


Link to post
Share on other sites

@Luigi, I've considered using Opt(), but I know where I'm defining my variables, so it's not too much of an issue. I guess I'll just start using global/local for all variables regardless of how obvious it's scope is.

Share this post


Link to post
Share on other sites
21 minutes ago, Pured said:

@Luigi, I've considered using Opt(), but I know where I'm defining my variables, so it's not too much of an issue. I guess I'll just start using global/local for all variables regardless of how obvious it's scope is.

It will matter once you will have a script where you do care about the speed. So better do it from the beginning and establish a good way of coding,

Share this post


Link to post
Share on other sites

@Hurley, Thanks. I never thought about its impact on speed.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

they dont require anything special, see:

the return of any function at all that returns an array and Dim.  And scoping isnt going to help with anything at all except maintenance of the code.  Also, there is magic that happens behind the scenes:

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

@iam, Okay, so speed isn't actually affected since the compiler defines them before runtime, correct?

Share this post


Link to post
Share on other sites

there are like 2 people max that can give you an answer that isnt borderline retarded (or at least a blind guess).  I am not one of those people.  However, I would never accept a speed argument without a Proof of Concept.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

11 minutes ago, Pured said:

@iam, Okay, so speed isn't actually affected since the compiler defines them before runtime, correct?

Judge by yourself - I know the differnce is mininmal but think in bigger dimensions:

Global $giTest
Global $giTest_ByRef
$iLoopMax = 5000000

$iTimer = TimerInit()
For $iLoop = 0 To $iLoopMax
    _Main_V1()
Next
ConsoleWrite(TimerDiff($iTimer) & @CRLF)

$iTimer = TimerInit()
For $iLoop = 0 To $iLoopMax
    _Main_V2()
Next
ConsoleWrite(TimerDiff($iTimer) & @CRLF)

$iTimer = TimerInit()
For $iLoop = 0 To $iLoopMax
    _Main_V3()
Next
ConsoleWrite(TimerDiff($iTimer) & @CRLF)


$iTimer = TimerInit()
For $iLoop = 0 To $iLoopMax
    _Main_V4($giTest_ByRef)
Next
ConsoleWrite(TimerDiff($iTimer) & @CRLF)

Func _Main_V1()
    $giTest = Random()
    Return $giTest
EndFunc

Func _Main_V2()
    $iTest = Random()
    Return $iTest
EndFunc

Func _Main_V3()
    Local $iTest = Random()
    Return $iTest
EndFunc

Func _Main_V4(ByRef $iInt)
    $iInt = Random()
EndFunc

//Edit: So more or less I was really wrong.

//Edit2: At least when it comes to usage of memory it might be better if you use a local scope.

Edited by HurleyShanabarger
Stupid script :D

Share this post


Link to post
Share on other sites

I ran the test 3 times, with the functions in 3 different orders.  Tell me if you can identify the trend?

;~ 0.00950340526017283 - v1
;~ 0.008743132839359 - v2
;~ 0.00380136210406913 - v3

;~ 0.0380136210406913  - v2
;~ 0.022047900203601 - v3
;~ 0.0167259932579042 - v1

;~ 0.0106438138913936 - v3
;~ 0.00532190694569678 - v1
;~ 0.00380136210406913 - v2

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
5 minutes ago, iamtheky said:

I ran the test 3 times, with the functions in 3 different orders.  Tell me if you can identify the trend?

;~ 0.00950340526017283 - v1
;~ 0.008743132839359 - v2
;~ 0.00380136210406913 - v3

;~ 0.0380136210406913  - v2
;~ 0.022047900203601 - v3
;~ 0.0167259932579042 - v1

;~ 0.0106438138913936 - v3
;~ 0.00532190694569678 - v1
;~ 0.00380136210406913 - v2

 

Edit my script - there was a mistake inside; see my edit.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

still no matter which way i run them, no matter what else is going on with my system, they take progressively longer to execute.  Either the test is not accurate or back to my hypothesis, scoping does not matter in relationship to speed.

;~ 6990.2510267479 - v1
;~ 11745.9158165554 - v2
;~ 11969.2507819402 - v3
;~ 12215.0099823769 - v4


;~ 16821.4408987028 - v4
;~ 21365.0269364519 - v3
;~ 18593.8989658775 - v2
;~ 18698.0642703894 - v1

;~ 3788.5214830178 - v3
;~ 5206.71693081065 - v4
;~ 5446.71326629758 - v1
;~ 5676.78234464974 - v2

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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