Sign in to follow this  
Followers 0
Djordhan

Does setting variables add processing time ?

8 posts in this topic

Hi there,

Forgive my ignorance but does setting a variables before using increase processing time at runtime ?

Ex: Is sample1 faster than sample2 at runtime ?

Sample1:

MsgBox(0,"Hello", "World")

Sample2:

$sTitle = "Hello"

$sText = "World"

MsgBox(0,$sTitle, $sText)

Thank you.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

If you change the script just slightly, you can test that. If you use MsgBox, it is hard to actually test because you are taking into account how long it takes to click ok. So I change them to this:

;Sample1:
ConsoleWrite('Hello World')

which takes 0.324s to run

and this

;Sample 2
$sTitle = "Hello"
$sText = "World"
ConsoleWrite($sTitle & $sText)

which takes 0.430s

Edit:Scite has a built in timer that lets you see the time elapsed to complete the script. Note however that the times varied, and are not exactly the same each time.

Edited by dufran3

Share this post


Link to post
Share on other sites

So setting variables before using them in function to enhance code readability and usability does in fact increase processing time ? I thought it was just compiling time. I'll take care not to overuse this then :) Thanks!

Share this post


Link to post
Share on other sites

So setting variables before using them in function to enhance code readability and usability does in fact increase processing time ? I thought it was just compiling time. I'll take care not to overuse this then :) Thanks!

This is quite ridiculous.

You could declare thousands of variables in less time than it takes to blink, declare all you like..

In fact use Opt("MustDeclareVars",1) at the top of your script to force you in to declaring them when you use variables and create functions it is much better and safer to declare local Variables in functions.

Share this post


Link to post
Share on other sites

Sorry to sound ridiculous.. I was talking about those times when you assign value to a variable only for better readability, instead of using the value directly in function.

I always declare my variables before using them.

But I agree that saving a split second doesn't really matter.

I'll take care not to overuse this then

This was more like if I have a script that runs thousands of sql queries and the script is ran thousands of time overnight. The difference could became more than a split second if I set a variable for each query string before passing it to the function.

Share this post


Link to post
Share on other sites

You can always re-use a variable as well and that is really what you should always attempt to do. When you are finished with it the first time then give it a new value.

Local $sFile = @DeskTopDir & "\Test.txt"
Local $sTxt = "SomeText"
FileWriteLine($sFile, $sTxt)
$sTxt = "More text" ; here we re-use $sTxt
FileWrite($sFile, $sTxt)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Sorry to sound ridiculous.. I was talking about those times when you assign value to a variable only for better readability, instead of using the value directly in function.

I always declare my variables before using them.

But I agree that saving a split second doesn't really matter.

This was more like if I have a script that runs thousands of sql queries and the script is ran thousands of time overnight. The difference could became more than a split second if I set a variable for each query string before passing it to the function.

I will quite often use a variable to hold a long string, particularly with an SQL query, I find that it is easer to spot mistakes and make changes, they don't need to be in a variable but it does make it more readable and easier to spot the mistakes. It's also good if your spreading the data over multiple lines to chuck it in a variable.

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