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

how to speed up collections iteration

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?
Jul 27 '06 #1
7 1284
You might try Directcast instead of ctype
regards
Michel
"Peter" <pc*****@nospam.nospamschreef in bericht
news:%2***************@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?


Jul 27 '06 #2
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" <pc*****@nospam.nospamschreef in bericht
news:%2***************@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?
Jul 27 '06 #3
try a typed collection (start from a generic (Of TranslateImport), to avoid
runtime cast...
"Peter" <pc*****@nospam.nospamwrote in message
news:%2***************@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?


Jul 27 '06 #4

"Trapulo" <tr*****@noemail.noemailwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
try a typed collection (start from a generic (Of TranslateImport), to
avoid runtime cast...
"Peter" <pc*****@nospam.nospamwrote in message
news:%2***************@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
Jul 27 '06 #5
>>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.ma.../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
Jul 27 '06 #6

<to**************@uniroma1.itwrote in message
news:11*********************@m79g2000cwm.googlegro ups.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" <pc*****@nospam.nospamschreef in bericht
news:%2***************@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
Jul 28 '06 #7
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
"Peter" <pc*****@nospam.nospamwrote in message
news:OO**************@TK2MSFTNGP02.phx.gbl...
|
| <to**************@uniroma1.itwrote in message
| news:11*********************@m79g2000cwm.googlegro ups.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" <pc*****@nospam.nospamschreef in bericht
| >news:%2***************@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
|
|
Jul 28 '06 #8

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

Similar topics

17
by: PDQBach | last post by:
Hello, im a visual c++ und borland c++builder newbie. i have witten a simple mandelbrot algorithm and compiled it with both vc++ (mfc) and cbuilder (vcl) (same code besides the drawing part)....
2
by: Vishal Somaiya | last post by:
Hello I am trying read from a xml file, pull the values in a object and then add the object to an ArrayList. I am using a 'while' loop to move through each node in the xml file and pulling the...
1
by: Mark | last post by:
Is there an online site where the various methods of storing arrays of classes and variables are explained? For example, I'd love a grid that showed the various pluses and minues of an Array,...
6
by: Chua Wen Ching | last post by:
Hi there, 1) I am looking for the best collections techniques to be used in my program. Is hashtable or arraylist or any .net collection, which is the fastest when adding, getting out data???...
12
by: Marty | last post by:
Hi, I am building an application that need high speed of execution. There is a lot of string parsing that I do in VB.NET. Do I gain in speed if all that string parsing is made in a dll coded...
1
by: Tim T. | last post by:
I'm currently working on a report to forecast production for finished goods. The user can select one or more items to forecast. In addition, they may select one or more warehouses to view...
5
by: Matt | last post by:
Does anyone know or care to find out the speed difference between a directcast call and a direct call? for instance, i might have these lines of code: dim j as collections.arraylist ...
8
by: Julien Fiore | last post by:
Hi, I wrote a function to compute openness (a digital elevation model attribute). The function opens the gdal-compatible input raster and reads the values in a square window around each cell. The...
5
by: WebSnozz | last post by:
Some collections are such that efficient search algorithms work on them such as binary search if the collection is a type which is sorted. I'm wondering how LINQ searches these collections and if...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.