473,396 Members | 1,938 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,396 software developers and data experts.

speed about foreach , copy list to an arry then loop

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 loop though it?

Thanks,
Ryan Liu
Aug 22 '06 #1
2 3341
Ryan Liu wrote:
I heard foreach is slower then use loop directly, is that really true?
It depends on *exactly* which type is involved. foreach uses an
iterator, which in some cases will be faster than an indexer, and in
some cases will be slower. It's almost never a significant part of
performance though.
When loop though a list in a multiple thread environment, is that a good
practice to copy this list to an array the loop though it?
Not if all threads are just reading from the list.

Jon

Aug 22 '06 #2
A common question; simply: it depends on the collection! For simple arrays,
for instance, I believe the indexer usage is marginally faster - however,
the difference is very small. For more complex objects you should generally
assume that the iterator (foreach) knows what it is doing and is a good way
to iterate through the data. As an example, for a linked-list, there can
(depending on implementation) be a huge difference between indexer access
and iterator access - the iterator being O(n), and indexer O(n^2) - i.e.
*hugely* faster to use the iterator. And IIRC LinkedList<Tdoes not provide
an indexer - I'm talking about the concept, not any specific implementation.

Also bear in mind that IEnumerable[<T>] only supports foreach (not indexer).
foreach is a good way of coding; unless you have real numbers pointing to
this as a performance issue, I'd stick with it.

In the threaded environment, it depends on what the threads are doing. If
all threads are reading, then fine; use concurrent iterators. If you have a
mixture of reader(s) and writer(s) then you neeed to start thinking about
locking. I tend to avoid ReaderWriterLock as overkill, so yes, in this
situation (and if you intend doing non-trivial work for each item), it may
be prudent to: lock the collection; copy to an array; unlock; enumerate the
array. Assuming the other threads also lock for their critical access this
should keep things happy. Even then, I'd need to pinpoint this as a
bottleneck first, otherwise it may be a premature optimisation.

Marc
Aug 22 '06 #3

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

Similar topics

8
by: Rob Ristroph | last post by:
I have tried out PHP 5 for the first time (with assistance from this group -- thanks!). The people I was working with have a site that uses lots of php objects. They are having problems with...
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
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...
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
12
by: mart.franklin | last post by:
I hope I am not being too ignorant :p but here goes... my boss has written a bit of python code and asked me to speed it up for him... I've reduced the run time from around 20 minutes to 13 (not...
2
by: The.Relinator | last post by:
Hello, I am unable to get the following piece of code to work as desired. The fields valuable seems to contain all values however the vaules variable only contains the first assignied with $values...
3
by: Akira | last post by:
I noticed that using foreach is much slower than using for-loop, so I want to change our current code from foreach to for-loop. But I can't figure out how. Could someone help me please? Current...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
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
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
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
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,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.