|
-
Dec 17th, 2004, 11:34 AM
#1
Thread Starter
Banned
speed?
i was rading an article about optimizing speed
i was just wondering is
Dim A As Long
A = 1&
faster than
Dim A As Long
A = 1
Last edited by nareth; Dec 17th, 2004 at 03:54 PM.
-
Dec 17th, 2004, 11:48 AM
#2
Re: speed?
I believe it is faster. But I think it may make sence to use & next to a variable rather than a number. What I mean by this is this right here:
Dim A As Long, B as Long
B& = 1
A& = B&
Simply saying A = 1 since variables represent memory locations to store data just shows that you are storing the value 1 into the memory location of A. Values themselves don't really have a data type. But Variables on the other hand do to represent the type of data to store and allocate a number of bytes for that memory location depending on what data type you declared it as. Like 2 Bytes for Integer variables and 4 Bytes for Long variables has been allocated for that memory location.
Now A = B on the other hand, you are saying that you want the memory location's data to be copied into A. Putting the & (Long data type symbol) next to all the variables that are declared as long will let the compiler know what data types they are and prevent conversions from one data type to another. In my opinion, A& = B& will be saying that B's 4 byte long integer register's data will be copied into A's 4 byte long integer register. Saying A = B on the other hand will be saying B's unknown register's data will be copied into A's unknown register. And extra assembly code will be added to convert them to their proper data types, thus slowing things down.
I'm know assembly guru or anything like that, but I believe they can answer that better than I can.
Last edited by Jacob Roman; Dec 17th, 2004 at 11:52 AM.
-
Dec 17th, 2004, 11:53 AM
#3
Thread Starter
Banned
Re: speed?
oh ok thank you, i was thinking something like that.
-
Dec 17th, 2004, 12:14 PM
#4
Re: speed?
I made a test with these two codes:
Code:
For X = 1 To ITERATIONS
A& = 1&
Next X
Code:
For X = 1 To ITERATIONS
A = 1
Next X
There was no speed difference at all. Compiled, all advanced optimizations turned on, ten million iterations and ran many times.
-
Dec 17th, 2004, 12:30 PM
#5
Thread Starter
Banned
Re: speed?
can you post a img of the options?
-
Dec 17th, 2004, 12:37 PM
#6
Re: speed?
In my opinion, A& = B& will be saying that B's 4 byte long integer register's data will be copied into A's 4 byte long integer register. Saying A = B on the other hand will be saying B's unknown register's data will be copied into A's unknown register. And extra assembly code will be added to convert them to their proper data types, thus slowing things down.
If you are using Option Explicit and have declared A As Long and B As Long, no data type conversion will be done. Compiler is smart enough to optimize that.
-
Dec 17th, 2004, 12:40 PM
#7
Thread Starter
Banned
Re: speed?
 Originally Posted by nareth
can you post a img of the options?
?????
-
Dec 17th, 2004, 12:58 PM
#8
Thread Starter
Banned
Re: speed?
also another question..
If UCase$(Account.Name) <> UCase$(Param(1)) Then
strcomp(UCase$(Account.Name),UCase$(Param(1)),vbTextCompare)
If UCase(Account.Name) <> UCase(Param(1)) Then
strcomp(UCase(Account.Name),UCase(Param(1)))
wich is faster?
-
Dec 17th, 2004, 01:07 PM
#9
Re: speed?
can you post a img of the options?
I don't understand what you mean with that.
The one with $ is faster. In this case, UCase$ is string based, while UCase is variant based. There will be data type conversion when there is no $.
Then... as for the optimization, you have lots and lots to improve there.
strcomp(UCase$(Account.Name),UCase$(Param(1)),vbTextCompare)
If you are doing UCase$(), you don't need to do text compare, because of UCase$. You could actually take UCase$() out and just have text compare, because it is faster than calling UCase$() two times.
Then about the fastest comparison you can get:
Code:
'first check the length of the string (LenB is faster than Len)
If LenB(Account.Name) = LenB(Param(1)) Then
'then check if we can find it
If InStr(1, Account.Name, Param(1), vbTextCompare) = 1 Then
'it is a match
End If
End If
Last edited by Merri; Dec 17th, 2004 at 01:14 PM.
-
Dec 17th, 2004, 01:10 PM
#10
Thread Starter
Banned
Re: speed?
that helps alot merri thank you
-
Dec 17th, 2004, 01:15 PM
#11
Thread Starter
Banned
Re: speed?
i also red that mid$ is faster than let
so: Let DBHost = GetPrivateProfileString(Main.Name, "DBHost", DBHost, App.Path & "\settings.ini")
is slower than: Let Mid$(DBHost, 1) = GetPrivateProfileString(Main.Name, "DBHost", DBHost, App.Path & "\settings.ini")
?
-
Dec 17th, 2004, 02:00 PM
#12
Re: speed?
Looks like I really should write the benchmarking article
-
Dec 17th, 2004, 02:03 PM
#13
Thread Starter
Banned
Re: speed?
what do you mean with benchmarking...
if its about let . just stfu and leave me with let i like it. otherwise sorry
-
Dec 17th, 2004, 02:09 PM
#14
Re: speed?
 Originally Posted by Merri
I made a test with these two codes:
Code:
For X = 1 To ITERATIONS
A& = 1&
Next X
Code:
For X = 1 To ITERATIONS
A = 1
Next X
There was no speed difference at all. Compiled, all advanced optimizations turned on, ten million iterations and ran many times.
Merri, I already said that values have no data type, but variables do. Doing 1& will not make a difference at all. And why did you use a for loop? That's not the way to find out which is faster. Have you seen my performance test using the GetQueueStatus API in my faster DoEvents methods located in the code bank?
-
Dec 17th, 2004, 02:12 PM
#15
Thread Starter
Banned
Re: speed?
is a do while loop faster than a for loop?
-
Dec 17th, 2004, 02:17 PM
#16
Re: speed?
Well yeah. But if DoEvents is in there, a major performance it will result because DoEvents is slow. There are faster methods. But both kinds of loops are ok anyways because one is infinite and the other one is not. For Loops and Do Loops are two different things and used for different operations. Like you can't use a For loop for a game loop or a rendering loop.
-
Dec 17th, 2004, 02:19 PM
#17
Thread Starter
Banned
Re: speed?
could you give me the link of the custom doevents .. thanks lot
-
Dec 17th, 2004, 02:21 PM
#18
Re: speed?
I edited my post above yours. I forgot some things to add. Here's the URL though:
http://www.vbforums.com/showthread.php?t=315416
-
Dec 17th, 2004, 02:26 PM
#19
Thread Starter
Banned
Re: speed?
... so
Do While I < J
I = I + 1
Loop
is faster than
For I = 0 To J
Next
-
Dec 17th, 2004, 02:30 PM
#20
Frenzied Member
Re: speed?
here is the answer:
A = 1&
tells the compiler to store 1 as a long, otherwise it would have been stored as an integer. it saves a type conversion.
edit: oh, and the for next is faster than the do while
-
Dec 17th, 2004, 02:48 PM
#21
Re: speed?
Jacob: because I used sTime class module (available somewhere from the first contest thread) which gives very accurate results on how many ms a code ran. I see no problem running the code a lot of times to see if there is a difference.
You also ignored my comment on the fact compiler is wise enough to detect defined variables and thus prevent datatype conversion when it is not required (you said A& = B& would have difference to A = B, which isn't true and I also tested this).
dis: depends on usage. On raw benchmarking Do loop is faster. I've sometimes encountered situatations when For loop worked faster, but I can't recall when it was. I'm not sure about it either, memory always gets muddy over time.
Also... I noticed no difference between 1 and 1&, so I believe compiler did notice this situatation and optimize it to be a long instead of an integer.
-
Dec 17th, 2004, 03:11 PM
#22
Re: speed?
I did read your thing, Merri, when you said that the compiler is wise enough to detect defined variables. But you ignored what I said when values don't have a data type, variables do. Which is why you will not see a difference in 1 and 1&.
I'm going to do an experiment with A = B and A& = B& with my performance test...
-
Dec 17th, 2004, 03:19 PM
#23
Thread Starter
Banned
Re: speed?
 Originally Posted by Merri
depends on usage. On raw benchmarking Do loop is faster. I've sometimes encountered situatations when For loop worked faster, but I can't recall when it was.
may this be the for each?
-
Dec 17th, 2004, 03:23 PM
#24
Re: speed?
What?!! That's odd. A = B is faster than A& = B&. Look at my performance test:
VB IDE
----------------
A = B ---> 1061721
A& = B& ---> 1036673
EXE
----------------
A = B ---> 1109474
A& = B& ---> 1107244
So A = B is faster.
-
Dec 17th, 2004, 03:27 PM
#25
Thread Starter
Banned
Re: speed?
thats because they are both varient( ?)
and on the iretator it converts both varients to long first
-
Dec 17th, 2004, 03:29 PM
#26
Re: speed?
Wrong. They have both been declared as Long!
And get this. I tried A = 1 as a test and check this out! Using numbers with & makes them faster.
VB IDE
----------------
A = 1 ---> 1008852
A = 1& ---> 1019759
EXE
-----------------
A = 1 ---> 1145979
A = 1& ---> 1150383
-
Dec 17th, 2004, 03:34 PM
#27
Re: speed?
Well, & and the like are only support stuff from the old days, so I don't wonder if those haven't been optimized in the VB6's C/C++ based, hacked compiler.
Also, those values can have a data type. When I added # instead of &, the speed fell down from the hill. It did make a datatype conversion. But if you don't declare anything for the value explicitly, compiler will use the best possible match: Byte for a Byte, Integer for an Integer and so on. So in the end, there is no point in adding an explicit datatype for the value.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|