473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Declaring Vars in a loop

Is there any difference (memory/speed wise) between these two snipits. I
always like to write in snipit 2 style, but was just curious. I'm scared
I'd declare a thousand pointers when I don't need to.

Snip 1:
Dim Obj as object
For ii as integer = 0 to 1000
Obj = GetSomeTypeOfObject()
DoSomeThingWithObject(Obj)
Next

Snip 2:
For ii as integer = 0 to 1000
Dim Obj as object = GetSomeTypeOfObject()
DoSomeThingWithObject(Obj)
Next

Thanks for the insite.
Chris
Nov 21 '05 #1
5 1964
"Chris" <NO****@NOTSPAM.COM> schrieb:
Is there any difference (memory/speed wise) between these two snipits. I
always like to write in snipit 2 style, but was just curious.


Compile both versions and take a look at the IL using the "ILDASM.EXE"
tool...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #2
"Chris" <NO****@NOTSPAM.COM> schrieb:
Is there any difference (memory/speed wise) between these two snipits. I
always like to write in snipit 2 style, but was just curious.


Compile both versions and take a look at the IL using the "ILDASM.EXE"
tool...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #3
On Tue, 26 Oct 2004 15:02:50 -0500, Chris wrote:
Is there any difference (memory/speed wise) between these two snipits. I
always like to write in snipit 2 style, but was just curious. I'm scared
I'd declare a thousand pointers when I don't need to.

Snip 1:
Dim Obj as object
For ii as integer = 0 to 1000
Obj = GetSomeTypeOfObject()
DoSomeThingWithObject(Obj)
Next

Snip 2:
For ii as integer = 0 to 1000
Dim Obj as object = GetSomeTypeOfObject()
DoSomeThingWithObject(Obj)
Next

Thanks for the insite.
Chris


As far as Dim'ing the object, you are still creating 1000 objects. In Snip
2, the Obj variable is only visible inside the For loop. Wheras with the
first, the Object is visible after the for loop exits.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 21 '05 #4
On Tue, 26 Oct 2004 15:02:50 -0500, Chris wrote:
Is there any difference (memory/speed wise) between these two snipits. I
always like to write in snipit 2 style, but was just curious. I'm scared
I'd declare a thousand pointers when I don't need to.

Snip 1:
Dim Obj as object
For ii as integer = 0 to 1000
Obj = GetSomeTypeOfObject()
DoSomeThingWithObject(Obj)
Next

Snip 2:
For ii as integer = 0 to 1000
Dim Obj as object = GetSomeTypeOfObject()
DoSomeThingWithObject(Obj)
Next

Thanks for the insite.
Chris


As far as Dim'ing the object, you are still creating 1000 objects. In Snip
2, the Obj variable is only visible inside the For loop. Wheras with the
first, the Object is visible after the for loop exits.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 21 '05 #5
Aren't you really just creating a single reference to an object, obj.
Doesn't the loop just reassign an object reference for GetSomeTypeOfObject()
to obj. You can't have more than one obj variable can you? I was surprised
that the dim inside the loop runs.
..

"Chris Dunaway" <"dunawayc[[at]_lunchmeat" wrote:
On Tue, 26 Oct 2004 15:02:50 -0500, Chris wrote:
Is there any difference (memory/speed wise) between these two snipits. I
always like to write in snipit 2 style, but was just curious. I'm scared
I'd declare a thousand pointers when I don't need to.

Snip 1:
Dim Obj as object
For ii as integer = 0 to 1000
Obj = GetSomeTypeOfObject()
DoSomeThingWithObject(Obj)
Next

Snip 2:
For ii as integer = 0 to 1000
Dim Obj as object = GetSomeTypeOfObject()
DoSomeThingWithObject(Obj)
Next

Thanks for the insite.
Chris


As far as Dim'ing the object, you are still creating 1000 objects. In Snip
2, the Obj variable is only visible inside the For loop. Wheras with the
first, the Object is visible after the for loop exits.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.

Nov 21 '05 #6

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

Similar topics

0
by: james | last post by:
I am new to php and need some help getting the session variables into include files. (after-thought, Sorry for the drawn out post but I really, really need help....;) Here's what I'm doing.. ...
3
by: ken.boss | last post by:
I am a python newbie, and am grappling with a fundamental concept. I want to modify a bunch of variables in place. Consider the following: >>> a = 'one' >>> b = 'two' >>> c = 'three' >>>...
2
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is allocating memory for a new variable more...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
4
by: jhightow | last post by:
The following code : For i as system.Int32 = 0 to 4 If i < 2 Then Dim s as System.String s &= i.ToString Console.WriteLine(s) End If Next
8
by: rendle | last post by:
I have a MSIL/performance question: Is there any difference between declaring a variable once and assigning to it multiple times, and declaring and assigning multiple times? For example: //...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
6
by: =?Utf-8?B?QUw=?= | last post by:
Hi I usually stick to the convention of not declaring variables in my bodies of "loops" (including foreach) ie int x; for (int i = 0; i < 10; i++) {
19
RMWChaos
by: RMWChaos | last post by:
Previously, I had used independent JSON lists in my code, where the lists were part of separate scripts. Because this method did not support reuse of a script without modification, I decided to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.