Re: How to loop through Hashtable keys without using foreach

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




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" <akira@xxxxxxxxxxxxx> 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] );
}

.



Relevant Pages

  • Threads and Directory Handles
    ... sub file_stats{ ... foreach my $file (keys %$files){ ... This is perl, v5.10.0 built for MSWin32-x86-multi-thread ...
    (comp.lang.perl.misc)
  • Re: sequence composition
    ... my %s;# a hash of arrays, to hold each line of sequence ... #a hash to hold the AA sequences. ... foreach my $k (keys %seq){ ...
    (perl.beginners)
  • Re: software engineering, program construction
    ... foreach my $site (keys %}) ... the charts to a PDF document for distribution. ...
    (comp.lang.perl.misc)
  • Re: OO new
    ... bless $self, $pkg; ... foreach (keys %opts) { ...
    (comp.lang.perl.misc)
  • Re: sort array by values
    ... of it's keys). ... but you can't tell the sort and search functions to ... Having [array] return a more useful ... foreach { ...
    (comp.lang.tcl)