By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,251 Members | 2,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,251 IT Pros & Developers. It's quick & easy.

How to loop through Hashtable keys without using foreach

P: n/a
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 code is here:

foreach ( string propertyName in ht.Keys )
{
this.setProperty( propertyName, ht[propertyName] );
}

Feb 1 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Feb 1, 8:56 am, "Akira" <a...@regonline.comwrote:
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 code is here:

foreach ( string propertyName in ht.Keys )
{
this.setProperty( propertyName, ht[propertyName] );

}
You "noticed" that using foreach is "much slower" than using a for
loop?

How did you "notice" that? Did you test your application and discover
that the foreach was taking too much time? Did you try it with a "for"
loop and discover that it was faster? Or is this just something you
read somewhere?

For collections, "foreach" is generally a better solution than "for":
it's easier to read, and it's often faster, because the operation
"find the next item" in a collection is often (but not always) faster
than "find the nth item". (The exception is collections like arrays,
in which the two operations differ little, if at all, in speed.)

On top of all of this, I am very, very doubtful that the act of
fetching the next element to process is somehow so costly that it
contributes more to the overall execution time than, say, whatever
you're doing with that next element once you have it. In my
experience, optimizing things like foreach vs for is a waste of time,
*unless* you have benchmarked your program and discovered that it is
in fact a problem.

Feb 1 '07 #2

P: n/a
Akira wrote:
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 code is here:

foreach ( string propertyName in ht.Keys )
{
this.setProperty( propertyName, ht[propertyName] );
}
In what situation did you see that a foreach loop is slower than a for
loop? There might be a small difference, but I think that you have to
have a very special situation to be able to even measure the difference.

To be able to use a for loop on the keys, you have to copy the keys to
an array to be able to access them by index:

ICollection keyCollection = ht.Keys;
string[] keys = new string[keyCollection.Count];
keyCollection.CopyTo(keys, 0);
for (int i = 0; i < keys.Length; i++) {
this.setProperty(keys[i], ht[keys[i]]);
}

This means that the CopyTo method will loop through the buckets in the
hashtable once to copy all the keys, which is about the same as the
enumerator used in the foreach loop does. After that you have another
loop, that, even if it's slightly faster than a foreach loop, never can
make up for the time lost when creating the array.

In this case, the for loop will be very much slower than a foreach loop
because what you are looping through is much better suited for an
enumerator than a counter.

In most cases where you can efficiently use either form of loop, you
will not be able to measure any difference, and in the few cases where
you could measure any difference, I'm not at all sure that it would
always be in the favour of a for loop.

You should use the form of loop that fits best for what you are doing
instead of limiting yourself to the one form that you believe is faster.
Save the cutting edge optimising to the code where it will do any
difference.

/Göran Andersson
_____
http://www.guffa.com
Feb 1 '07 #3

P: n/a

The problem isn't the foreach but how you're using the data. You're
looping through the keys and then again looking up each value by key
which is waisteful. Instead do this:

foreach(DictionaryEntry de in ht) {
this.setProperty(de.Key, de.Value);
}

You may need some casting depending on how setProperty is declared.

In general you don't want to replace for-each loops with indexed
loops.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On 1 Feb 2007 08:56:45 -0800, "Akira" <ak***@regonline.comwrote:
>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 code is here:

foreach ( string propertyName in ht.Keys )
{
this.setProperty( propertyName, ht[propertyName] );
}
Feb 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.