|
-
Mar 31st, 2004, 02:47 PM
#1
Thread Starter
Addicted Member
variables declaration
Can anyone tell the difference between the following 3 declarations:
1. dim i, j as long
2. dim i as long, j as long
3. dim i as long
dim j as long
?
-
Mar 31st, 2004, 03:07 PM
#2
Hyperactive Member
On 1, i will be declared as a variant. The other 2 are exactly the same apart from the layout difference of course.
-adehh
-
Mar 31st, 2004, 05:23 PM
#3
Thread Starter
Addicted Member
Thanks.
And yet another question: I want my app to be both efficient and economic. When should I use variables of type byte? integer? long?
-
Mar 31st, 2004, 05:35 PM
#4
Hyperactive Member
I rarely use bytes in my apps so I'm not sure in that area, I suppose it depends entirely on what you're doing.
When it comes to integer/long though I always go for long as I once read somewhere that longs are 32bit, apposed to integers being 16bit so longs are faster as it is 'their' environment. Although I'm not 100 percent on this, wait for a second opinion.
-adehh
-
Mar 31st, 2004, 05:39 PM
#5
Frenzied Member
Depends on how much memory you want each of your variables to use. You need to use what best defines what will be inside of it.
Originally posted by jsbach
Thanks.
And yet another question: I want my app to be both efficient and economic. When should I use variables of type byte? integer? long?
-
Mar 31st, 2004, 05:56 PM
#6
Intel type systems use 32 bits efficiently, but I doubt there is a cost for using a 16 bit integer over a 32. Therefore, you might actually save a little space with an integer, and expect no loss of efficiency. However, I believe that using a Byte will not actually save anything. I would expect that a byte type would actually be either an integer or a long, with the upper bytes masked out.
-
Mar 31st, 2004, 08:39 PM
#7
To take a little further I can assure you that VB's Long type performs just as fast as Integer so there is actually no reason not to use Long for at least avoiding some overflow errors.
-
Apr 1st, 2004, 10:18 AM
#8
Thread Starter
Addicted Member
Originally posted by RhinoBull
To take a little further I can assure you that VB's Long type performs just as fast as Integer so there is actually no reason not to use Long for at least avoiding some overflow errors.
But isn't it a waste of system resources?
-
Apr 1st, 2004, 10:24 AM
#9
You might say that it wastes two bytes to use a long when an int would suffice. However, I don't actually know whether that is true. I could imagine systems that manage memory such that each variable is stored on some boundary. This could mean that an int is stored in four bytes, despite using only two.
This is the case for booleans. Despite the fact that a boolean could be stored in one bit, eight booleans will not be packed into a byte, but will instead each take up at least two bytes.
If your design is so tight that you are concerned about using four bytes when you could get away with two, you might want to look into how the underlying architecture is actually managing memory. VB doesn't usually get into that.
-
Apr 1st, 2004, 10:24 AM
#10
Only if you use tens (or hundreds) of thousands of them.. there is so much memory available that having an extra few bytes used up isn't an issue.
-
Apr 1st, 2004, 10:25 AM
#11
Fanatic Member
i believe resources are wasted in code which is not optimized. just consider declaring variables only if you need them.
for instance, you might prefer reusing a same variable more than declaring two of the same kind:
VB Code:
Dim a As Long
Dim b As Long
Dim c As Long
b = a + 3
c = b + 4
will give same results as
VB Code:
Dim a As Long
Dim c As Long
c = a + 3 + 4
stupid example but you get the point. same goes for functions and loops: choose the best ones. use byte arrays for fast computations more than strings, on which VB is particularly slow.
RhinoBull: did you do some tests on that? i found this quite interesting.
Last edited by wildcat_2000; Apr 1st, 2004 at 10:40 AM.
When your car breaks down,
close all windows and retry 
=> please rate all users posts! <=
-
Apr 6th, 2004, 02:47 PM
#12
Thread Starter
Addicted Member
And one more question:
What's the difference between:
const PI = 3.14159
and
const PI as long = 3.14159
?
-
Apr 7th, 2004, 03:57 AM
#13
Well for a start, Long is the shortened version of Long Integer, so it cannot hold decimal places. You should use Single or Double.
Using a specific data type is a good idea if possible, as VB may not make the right assumptions about what data type is should be (although I have never tested it myself, and have never heard of any problems).
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
|