Home All Groups Group Topic Archive Search About

how to speed up collections iteration

Author
27 Jul 2006 5:30 AM
Peter
I have the following code:
_transColl is a collection of classes and it contains about 42000 objects
My question is there any way I can make this code run faster?

I have found out that line 2 is realy expensive (time wise) :

'------------------------------------------------------------------

1            For n As Int32 = 1 To Me._transColl.Count - 1
2                ti = CType(Me._transColl(n), TranslateImport)
3               If ti.Key.Equals(key) Then
4                    ti.TranslatedValue = val
5                End If
6            Next

'
' the following code is useless by it's very fast, proving that the line 2
is the culprint
' I've moved the line 2 out out the loop
'
            ti = CType(Me._transColl(0), TranslateImport)

            For n As Int32 = 1 To Me._transColl.Count - 1
                If ti.Key.Equals(key) Then
                    ti.TranslatedValue = val
                End If
            Next


Thanks for any input?

Author
27 Jul 2006 6:16 AM
Michel Posseth [MCP]
You might try Directcast  instead of ctype


regards


Michel


Show quoteHide quote
"Peter" <pczurak@nospam.nospam> schreef in bericht
news:%23Jt9J4TsGHA.148@TK2MSFTNGP05.phx.gbl...
>I have the following code:
> _transColl is a collection of classes and it contains about 42000 objects
> My question is there any way I can make this code run faster?
>
> I have found out that line 2 is realy expensive (time wise) :
>
> '------------------------------------------------------------------
>
> 1            For n As Int32 = 1 To Me._transColl.Count - 1
> 2                ti = CType(Me._transColl(n), TranslateImport)
> 3               If ti.Key.Equals(key) Then
> 4                    ti.TranslatedValue = val
> 5                End If
> 6            Next
>
> '
> ' the following code is useless by it's very fast, proving that the line 2
> is the culprint
> ' I've moved the line 2 out out the loop
> '
>            ti = CType(Me._transColl(0), TranslateImport)
>
>            For n As Int32 = 1 To Me._transColl.Count - 1
>                If ti.Key.Equals(key) Then
>                    ti.TranslatedValue = val
>                End If
>            Next
>
>
> Thanks for any input?
>
>
Author
27 Jul 2006 7:35 AM
tommaso.gastaldi
I like to avoid indices every time it is possible (2005), code is
clearer imho:

        For Each TranslateImport  As TranslateImport In Me._transColl
            with TranslateImport
                 If .Key = key Then .TranslatedValue = Val()
             end with
        Next TranslateImport

-tom

Michel Posseth  [MCP] ha scritto:

Show quoteHide quote
> You might try Directcast  instead of ctype
>
>
> regards
>
>
> Michel
>
>
> "Peter" <pczurak@nospam.nospam> schreef in bericht
> news:%23Jt9J4TsGHA.148@TK2MSFTNGP05.phx.gbl...
> >I have the following code:
> > _transColl is a collection of classes and it contains about 42000 objects
> > My question is there any way I can make this code run faster?
> >
> > I have found out that line 2 is realy expensive (time wise) :
> >
> > '------------------------------------------------------------------
> >
> > 1            For n As Int32 = 1 To Me._transColl.Count - 1
> > 2                ti = CType(Me._transColl(n), TranslateImport)
> > 3               If ti.Key.Equals(key) Then
> > 4                    ti.TranslatedValue = val
> > 5                End If
> > 6            Next
> >
> > '
> > ' the following code is useless by it's very fast, proving that the line 2
> > is the culprint
> > ' I've moved the line 2 out out the loop
> > '
> >            ti = CType(Me._transColl(0), TranslateImport)
> >
> >            For n As Int32 = 1 To Me._transColl.Count - 1
> >                If ti.Key.Equals(key) Then
> >                    ti.TranslatedValue = val
> >                End If
> >            Next
> >
> >
> > Thanks for any input?
> >
> >
Author
28 Jul 2006 6:03 AM
Peter
<tommaso.gasta***@uniroma1.it> wrote in message
Show quoteHide quote
news:1153985735.900504.25330@m79g2000cwm.googlegroups.com...
>I like to avoid indices every time it is possible (2005), code is
> clearer imho:
>
>        For Each TranslateImport  As TranslateImport In Me._transColl
>            with TranslateImport
>                 If .Key = key Then .TranslatedValue = Val()
>             end with
>        Next TranslateImport
>
> -tom
>
> Michel Posseth  [MCP] ha scritto:
>
>> You might try Directcast  instead of ctype
>>
>>
>> regards
>>
>>
>> Michel
>>
>>
>> "Peter" <pczurak@nospam.nospam> schreef in bericht
>> news:%23Jt9J4TsGHA.148@TK2MSFTNGP05.phx.gbl...
>> >I have the following code:
>> > _transColl is a collection of classes and it contains about 42000
>> > objects
>> > My question is there any way I can make this code run faster?
>> >
>> > I have found out that line 2 is realy expensive (time wise) :
>> >
>> > '------------------------------------------------------------------
>> >
>> > 1            For n As Int32 = 1 To Me._transColl.Count - 1
>> > 2                ti = CType(Me._transColl(n), TranslateImport)
>> > 3               If ti.Key.Equals(key) Then
>> > 4                    ti.TranslatedValue = val
>> > 5                End If
>> > 6            Next
>> >
>> > '
>> > ' the following code is useless by it's very fast, proving that the
>> > line 2
>> > is the culprint
>> > ' I've moved the line 2 out out the loop
>> > '
>> >            ti = CType(Me._transColl(0), TranslateImport)
>> >
>> >            For n As Int32 = 1 To Me._transColl.Count - 1
>> >                If ti.Key.Equals(key) Then
>> >                    ti.TranslatedValue = val
>> >                End If
>> >            Next
>> >
>> >
>> > Thanks for any input?
>> >
>> >
>

I have tried the fillowing in .NET 2.0 and it works realy fast, but in .NET
1.1 is slow the same as using for next loop.
Is there anything I can do to speed this up in .NET 1.1 ?


       For Each TranslateImport  As TranslateImport In Me._transColl
            with TranslateImport
                 If .Key = key Then .TranslatedValue = Val()
             end with
        Next TranslateImport
Author
28 Jul 2006 12:10 PM
Jay B. Harlow [MVP - Outlook]
Peter,
First I would question having 42000 objects in a collection.

| Is there anything I can do to speed this up in .NET 1.1 ?

What type of "collection" are you using? Array, ArrayList, VB.Collection, or
something else?

|       For Each TranslateImport  As TranslateImport In Me._transColl
|            with TranslateImport
|                 If .Key = key Then .TranslatedValue = Val()
|             end with
|        Next TranslateImport
Is the .key property unique in the collection? Have you considered a
HashTable (.NET 1.x) or Dictionary(Of T) (.NET 2.0) instead. Where .Key is
the key to the HashTable/Dictionary.

Alternatively: Can you sort the collection by .Key, then use a BinarySearch?


..NET 2.0 has made improvements in the performance of large DataSets, does
using a DataSet instead of a class & a collection improve performance?

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Peter" <pczurak@nospam.nospam> wrote in message
news:OO8ZdvgsGHA.4872@TK2MSFTNGP02.phx.gbl...
|
| <tommaso.gasta***@uniroma1.it> wrote in message
| news:1153985735.900504.25330@m79g2000cwm.googlegroups.com...
| >I like to avoid indices every time it is possible (2005), code is
| > clearer imho:
| >
| >        For Each TranslateImport  As TranslateImport In Me._transColl
| >            with TranslateImport
| >                 If .Key = key Then .TranslatedValue = Val()
| >             end with
| >        Next TranslateImport
| >
| > -tom
| >
| > Michel Posseth  [MCP] ha scritto:
| >
| >> You might try Directcast  instead of ctype
| >>
| >>
| >> regards
| >>
| >>
| >> Michel
| >>
| >>
| >> "Peter" <pczurak@nospam.nospam> schreef in bericht
| >> news:%23Jt9J4TsGHA.148@TK2MSFTNGP05.phx.gbl...
| >> >I have the following code:
| >> > _transColl is a collection of classes and it contains about 42000
| >> > objects
| >> > My question is there any way I can make this code run faster?
| >> >
| >> > I have found out that line 2 is realy expensive (time wise) :
| >> >
| >> > '------------------------------------------------------------------
| >> >
| >> > 1            For n As Int32 = 1 To Me._transColl.Count - 1
| >> > 2                ti = CType(Me._transColl(n), TranslateImport)
| >> > 3               If ti.Key.Equals(key) Then
| >> > 4                    ti.TranslatedValue = val
| >> > 5                End If
| >> > 6            Next
| >> >
| >> > '
| >> > ' the following code is useless by it's very fast, proving that the
| >> > line 2
| >> > is the culprint
| >> > ' I've moved the line 2 out out the loop
| >> > '
| >> >            ti = CType(Me._transColl(0), TranslateImport)
| >> >
| >> >            For n As Int32 = 1 To Me._transColl.Count - 1
| >> >                If ti.Key.Equals(key) Then
| >> >                    ti.TranslatedValue = val
| >> >                End If
| >> >            Next
| >> >
| >> >
| >> > Thanks for any input?
| >> >
| >> >
| >
|
| I have tried the fillowing in .NET 2.0 and it works realy fast, but in
..NET
| 1.1 is slow the same as using for next loop.
| Is there anything I can do to speed this up in .NET 1.1 ?
|
|
|       For Each TranslateImport  As TranslateImport In Me._transColl
|            with TranslateImport
|                 If .Key = key Then .TranslatedValue = Val()
|             end with
|        Next TranslateImport
|
|
Author
27 Jul 2006 10:23 AM
Trapulo
try a typed collection (start from a generic (Of TranslateImport), to avoid
runtime cast...


Show quoteHide quote
"Peter" <pczurak@nospam.nospam> wrote in message
news:%23Jt9J4TsGHA.148@TK2MSFTNGP05.phx.gbl...
>I have the following code:
> _transColl is a collection of classes and it contains about 42000 objects
> My question is there any way I can make this code run faster?
>
> I have found out that line 2 is realy expensive (time wise) :
>
> '------------------------------------------------------------------
>
> 1            For n As Int32 = 1 To Me._transColl.Count - 1
> 2                ti = CType(Me._transColl(n), TranslateImport)
> 3               If ti.Key.Equals(key) Then
> 4                    ti.TranslatedValue = val
> 5                End If
> 6            Next
>
> '
> ' the following code is useless by it's very fast, proving that the line 2
> is the culprint
> ' I've moved the line 2 out out the loop
> '
>            ti = CType(Me._transColl(0), TranslateImport)
>
>            For n As Int32 = 1 To Me._transColl.Count - 1
>                If ti.Key.Equals(key) Then
>                    ti.TranslatedValue = val
>                End If
>            Next
>
>
> Thanks for any input?
>
>
Author
27 Jul 2006 5:35 PM
Peter
Show quote Hide quote
"Trapulo" <trapulo@noemail.noemail> wrote in message
news:%2329esaWsGHA.4748@TK2MSFTNGP03.phx.gbl...
> try a typed collection (start from a generic (Of TranslateImport), to
> avoid runtime cast...
>
>
> "Peter" <pczurak@nospam.nospam> wrote in message
> news:%23Jt9J4TsGHA.148@TK2MSFTNGP05.phx.gbl...
>>I have the following code:
>> _transColl is a collection of classes and it contains about 42000 objects
>> My question is there any way I can make this code run faster?
>>
>> I have found out that line 2 is realy expensive (time wise) :
>>
>> '------------------------------------------------------------------
>>
>> 1            For n As Int32 = 1 To Me._transColl.Count - 1
>> 2                ti = CType(Me._transColl(n), TranslateImport)
>> 3               If ti.Key.Equals(key) Then
>> 4                    ti.TranslatedValue = val
>> 5                End If
>> 6            Next
>>
>> '
>> ' the following code is useless by it's very fast, proving that the line
>> 2 is the culprint
>> ' I've moved the line 2 out out the loop
>> '
>>            ti = CType(Me._transColl(0), TranslateImport)
>>
>>            For n As Int32 = 1 To Me._transColl.Count - 1
>>                If ti.Key.Equals(key) Then
>>                    ti.TranslatedValue = val
>>                End If
>>            Next
>>
>>
>> Thanks for any input?
>>
>>
>
>

Sorry I did not mention - I am running VS2003
Author
27 Jul 2006 6:22 PM
Jim Wooley
>>> I have the following code:
>>> _transColl is a collection of classes and it contains about 42000
>>> objects
>>> My question is there any way I can make this code run faster?
>>> I have found out that line 2 is realy expensive (time wise) :
>>>
>>> 1            For n As Int32 = 1 To Me._transColl.Count - 1
>>> 2                ti = CType(Me._transColl(n), TranslateImport)
> Sorry I did not mention - I am running VS2003

You can try DirectCast over CType, but be sure the values in your collection
are only the type you are trying to cast to. This is one of the key advantages
of List(of T) over ArrayList(). I recommend Sahil Malik's comparison at http://codebetter.com/blogs/sahil.malik/archive/2005/01/02/40506.aspx.

In general, I would question iterating over 42000 in your business layer
rather than directly on the database as you are likely suffering a greater
penalty with the memory footprint of that many objects, including the network
bandwidth of fetching them, than you are with the casting.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx