473,326 Members | 2,732 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,326 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 1957
"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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.