473,836 Members | 1,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looping variables - Integer vs Long

I did a test once using a looping variable, first dimmed as Integer,
then as Long. I found the Integer was quicker at looping. I knew this to
be true back in the 16 bit days where the CPU's (80286) word size was 16
bits same as an integer.

Now with a 32 bit CPU I would have expected the long to be faster as
it's the same size as the CPU's word size so wouldn't need sawing in
half like a magician's assistant to calculate on like an integer would.
So why is an Integer still faster than a Long?

Is VBA still stuck in 16 bits somewhere?
Nov 13 '05 #1
45 7502
Strange! I would have predicted the longs as faster.
Can you post your looping code so that we can test and see this marvel?

Nov 13 '05 #2
On Fri, 28 Oct 2005 12:43:07 +0100, Trevor Best <no****@localho st.invalid> wrote:
I did a test once using a looping variable, first dimmed as Integer,
then as Long. I found the Integer was quicker at looping. I knew this to
be true back in the 16 bit days where the CPU's (80286) word size was 16
bits same as an integer.

Now with a 32 bit CPU I would have expected the long to be faster as
it's the same size as the CPU's word size so wouldn't need sawing in
half like a magician's assistant to calculate on like an integer would.
So why is an Integer still faster than a Long?

Is VBA still stuck in 16 bits somewhere?


see
http://groups.google.com.au/group/mi...98955791f1afee
towards the end of the thread for some information on excodes, the "fuller explanation" cited there
seems to have vanished.

Nov 13 '05 #3
On Fri, 28 Oct 2005 12:43:07 +0100, Trevor Best <no****@localho st.invalid>
wrote:
I did a test once using a looping variable, first dimmed as Integer,
then as Long. I found the Integer was quicker at looping. I knew this to
be true back in the 16 bit days where the CPU's (80286) word size was 16
bits same as an integer.

Now with a 32 bit CPU I would have expected the long to be faster as
it's the same size as the CPU's word size so wouldn't need sawing in
half like a magician's assistant to calculate on like an integer would.
So why is an Integer still faster than a Long?

Is VBA still stuck in 16 bits somewhere?


What's really odd is that I read something from Microsoft once stating that
VBA converts bytes and integers to a Long for processing, so it's more
efficient to define everything that way to start with. I would have expected
Long to give the better performance.
Nov 13 '05 #4
Trevor Best <no****@localho st.invalid> wrote in
news:43******** *************** @news.zen.co.uk :
I did a test once using a looping variable, first dimmed as
Integer, then as Long. I found the Integer was quicker at looping.
I knew this to be true back in the 16 bit days where the CPU's
(80286) word size was 16 bits same as an integer.

Now with a 32 bit CPU I would have expected the long to be faster
as it's the same size as the CPU's word size so wouldn't need
sawing in half like a magician's assistant to calculate on like an
integer would. So why is an Integer still faster than a Long?

Is VBA still stuck in 16 bits somewhere?


Shouldn't one choose the variable type according to how many items
you're looping?

That is, if you need to loop 60K items, then you *can't* use an
integer, no?

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #5
lylefair wrote:
Strange! I would have predicted the longs as faster.
Can you post your looping code so that we can test and see this marvel?


OK, from my OP it was in the past <g>, I just did one now and the Long
was faster :-\

Output from code below:
Int Loop: 1.359
Lng Loop: 1.265625

Sub LoopTest()
Dim intLoop1 As Integer
Dim lngLoop1 As Long
Dim intLoop2 As Integer
Dim lngLoop2 As Long

Dim sngStart As Single
Dim sngETInt As Single
Dim sngETLng As Single

sngStart = Timer()
For intLoop1 = 1 To 10000
For intLoop2 = 1 To 10000
Next
Next
sngETInt = Timer() - sngStart

sngStart = Timer()
For lngLoop1 = 1 To 10000
For lngLoop2 = 1 To 10000
Next
Next
sngETLng = Timer() - sngStart

Debug.Print "Int Loop: " & sngETInt
Debug.Print "Lng Loop: " & sngETLng

End Sub
Nov 13 '05 #6
I should have tested it again before posting, it's now the other way
round, I'm using A2K2 BTW. If anyone who has 97 care to try it and
report back, the code is in response to Lyle's followup.

Nov 13 '05 #7
David W. Fenton wrote:
Shouldn't one choose the variable type according to how many items
you're looping?

That is, if you need to loop 60K items, then you *can't* use an
integer, no?


But if you know your loop is < 32K...

I generally use Longs anyway.
Nov 13 '05 #8

"Trevor Best" <no****@localho st.invalid> schreef in bericht news:43******** *************** @news.zen.co.uk ...
I should have tested it again before posting, it's now the other way
round, I'm using A2K2 BTW. If anyone who has 97 care to try it and
report back, the code is in response to Lyle's followup.


Here are my results from the Netherlands (tested in A97)

1st test:
Int Loop: 0,827875
Lng Loop: 0,79675

2nd test:
Int Loop: 0,8275
Lng Loop: 0,796375

3rd test:
Int Loop: 0,827375
Lng Loop: 0,81225

Arno R
Nov 13 '05 #9
Yes, I found that longs were faster.

BUTTTTTTTTTTTTT T ...

Well, it's morning here and I am still a little fuzzy headed but would
you, please, reverse the order of calling the loops (see below) and
tell me if longs are faster when the integer loop is called second.
(note that I have not changed the order of the debug.print calls).
My results don't make any sense to me at all, so maybe I am doing
something tres stupid!

Sub LoopTest()
Dim intLoop1 As Integer
Dim lngLoop1 As Long
Dim intLoop2 As Integer
Dim lngLoop2 As Long

Dim sngStart As Single
Dim sngETInt As Single
Dim sngETLng As Single

sngStart = Timer()
For lngLoop1 = 1 To 10000
For lngLoop2 = 1 To 10000
Next
Next
sngETLng = Timer() - sngStart

sngStart = Timer()
For intLoop1 = 1 To 10000
For intLoop2 = 1 To 10000
Next
Next
sngETInt = Timer() - sngStart

Debug.Print "Int Loop: " & sngETInt
Debug.Print "Lng Loop: " & sngETLng

End Sub

Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2709
by: Harry J. Smith | last post by:
How do you tell if two variables past as arguments to a subroutine are actually the same variable? In some languages you can compare their addresses. Visual Basic doesn't have pointers so what is the best way to do this? Public Sub SetToUI(X As MultiUI, Z As MultiUI) 'Z = X 'MultiUI is a user defined type (Multiple-precision unsigned integer) If Z = X Then Exit Sub ....
2
3604
by: Sonoman | last post by:
Hello everyone: I am new to Visual Basic and I have a problem that I cannot put my finger on. I am working on a school project and I am getting some serial data from a microprocessor. I have designed my "Do" loop to wait for each data set coming in at intervals of about 1/2 a second from the microprocessor. When I comment the following lines: bufferVar = ""
4
1687
by: Paul M | last post by:
Larry, i've got an xml file which has multiple cities as the top level item. When i read it in using XMLReader, and define the variables, as below, it finds the correct number of cities in the xml file, but repeats the first one 3 times. I'm sure its something minor but i just cant seem to figure it out. here is the XML, and source code below:
5
2422
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at the end of the iteration. Kindly note that I am not talking about dynamically allocating memory within a loop, which is a perfectly valid operation (albeit with a chance of resulting in a memory leak, unless we are careful). The most common...
122
5363
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) { /* array differs from value, do something*/
2
1398
by: starbuck | last post by:
Hi My first vb.net/asp.net application is taking shape now and I have started using session state to store user variables or type string, long and bool.. The VB6 app I am converting also makes great use of global variables as per the example below Structure Gen_setup Dim Nominal As Long
2
16102
by: pob | last post by:
Whats the difference between using a control or a listbox when looping thru a listbox. In example 1 it dims a listbox and an example 2 it dims a control. Please explain. Thanks in advance Example 1 from Allen Browne MVP Access To use a Multiselect list box for criteria for a report, you need to loop through its ItemsSelected collection to create a string that can act as the WhereCondition for your report.
3
1908
by: assgar | last post by:
Hi I am having problem with my loping. I don't know if I have chosen the correct approach. GOAL: I need to insert into a table event types for a specific date range. The calendar the event type is displayed on is divided into 15 minutes time intervals
7
4082
by: Tom Davis | last post by:
I am having a problem where a long-running function will cause a memory leak / balloon for reasons I cannot figure out. Essentially, I loop through a directory of pickled files, load them, and run some other functions on them. In every case, each function uses only local variables and I even made sure to use `del` on each variable at the end of the loop. However, as the loop progresses the amount of memory used steadily increases. I...
0
9825
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10846
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10595
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9379
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4458
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4021
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.