472,353 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

is there a foreach performance issue when using complex expressions?

Hi all,

If I have a expensive public property that exposes a collection, say
via a call to a database:

public ArrayList Complex
{
get
{
//expensive call to db
return DbResult;
}
}

and use this in a foreach:

foreach( object o in Complex )
{
...
}

Will the clr call Complex only once, and save a reference to the
direct variable actually exposed, or will it execute the code in the
get routine each iteration?

Thanks in advance,

Ben Joldersma
Sr. Software Engineer,
Citadel Media, Inc.
Nov 15 '05 #1
4 1645
Benjamin Joldersma <be**@theinsiders.com> wrote:

<snip>
Will the clr call Complex only once, and save a reference to the
direct variable actually exposed, or will it execute the code in the
get routine each iteration?


It calls it once, and gets an iterator. So no, you don't need to worry
about the time it takes to evaluate that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Hi Benjamin,

It call it once.

This is VERY easy to check ( I just did it ), just put a breakpoint inside
the get method , you will see it only enters once.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Benjamin Joldersma" <be**@theinsiders.com> wrote in message
news:8f**************************@posting.google.c om...
Hi all,

If I have a expensive public property that exposes a collection, say
via a call to a database:

public ArrayList Complex
{
get
{
//expensive call to db
return DbResult;
}
}

and use this in a foreach:

foreach( object o in Complex )
{
...
}

Will the clr call Complex only once, and save a reference to the
direct variable actually exposed, or will it execute the code in the
get routine each iteration?

Thanks in advance,

Ben Joldersma
Sr. Software Engineer,
Citadel Media, Inc.

Nov 15 '05 #3
Thanks so much for checking. I should have thought of that!
-----Original Message-----
Hi Benjamin,

It call it once.

This is VERY easy to check ( I just did it ), just put a breakpoint insidethe get method , you will see it only enters once.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Benjamin Joldersma" <be**@theinsiders.com> wrote in message
news:8f**************************@posting.google. com...
Hi all,

If I have a expensive public property that exposes a collection, say via a call to a database:

public ArrayList Complex
{
get
{
//expensive call to db
return DbResult;
}
}

and use this in a foreach:

foreach( object o in Complex )
{
...
}

Will the clr call Complex only once, and save a reference to the direct variable actually exposed, or will it execute the code in the get routine each iteration?

Thanks in advance,

Ben Joldersma
Sr. Software Engineer,
Citadel Media, Inc.

.

Nov 15 '05 #4
-----Original Message-----
Benjamin Joldersma <be**@theinsiders.com> wrote:

<snip>
Will the clr call Complex only once, and save a reference to the direct variable actually exposed, or will it execute the code in the get routine each iteration?
It calls it once, and gets an iterator. So no, you don't

need to worryabout the time it takes to evaluate that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 15 '05 #5

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

Similar topics

0
by: Randell D. | last post by:
Folks, Ever since reading an interesting article in Linux Format on PHP whereby suggested code writing was made that could enhance performance on a...
6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) -...
2
by: joerg | last post by:
Hello, we reached a quite interesting performance-limit while using JDBC type 4 to a DB2 V8.1 database. The DB2 is running on a Intel-based single...
5
by: Scott | last post by:
I have a customer that had developed an Access97 application to track their business information. The application grew significantly and they used...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } //...
27
by: Tripper | last post by:
Which is the better way to go and why? //trivial example List<string> strings = GetStrings(); foreach (string s in strings) { // some...
2
by: Ryan Liu | last post by:
Hi, I heard foreach is slower then use loop directly, is that really true? When loop though a list in a multiple thread environment, is that a...
1
by: jmacduff | last post by:
I have a performance issue related to regular expressions and caching , hopefully someone can point me in the right direction? I have a asp.net...
14
by: jehugaleahsa | last post by:
I have a rather complex need. I have a class that parses web pages and extracts all relevant file addresses. It allows me to download every pdf...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.