473,385 Members | 1,185 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.

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 1693
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 server, I've started testing various bits of...
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 ) - Linked Tables to SQL-Server 2000 over ODBC I used...
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 CPU-Server with Linux OS. What we are doing is...
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 the Upsizing Wizard to move the tables to SQL...
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) { } // backwards foeach(char c in 'a'..'z') { } // chars...
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 operation; } strings.ForEach(
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 good practice to copy this list to an array the...
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 web service that is called several million times a...
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 on a web page, for instance. I would like to...
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
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.