473,486 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Possible to optimize this?


I have a slight bottleneck in my code, the simplicity of which leads me to
believe it is either insurmountable, or easily speeded up. I have an
ArrayList of numbers (integers, which are in fact database primary keys).
In order to pass all of these keys into a stored procedure, I generate one
long space delimited string by concatenating the IDs and pass it into SQL
Server as an NTEXT field (the stored procedure then breaks the NTEXT field
into a table and does a join with another but thats by the way). When I
have, say, 16,000 primary keys in my list, the section below can take quite
a long time (seconds!). Is this reasonable? If not, can I speed it up any?
I'm guessing the compiler is quite smart at optimization and that something
as simple as this is already more or less maxed out.

My ideas are: perhaps iterating the collection with "For Each" might be
slower than directly indexing the array. Also, perhaps I should generate a
string of the correct length and then modify it rather than relying on the
runtime to resize or re-create the string on each iteration (but strings are
immutable, right?). Perhaps there is a different way I can convert an
integer to a string that is more efficient than "ToString".

An ideas?

(theNodeIDs is an ArrayList, theNodeIDsString is a string)....

For Each theNodeID As Integer In theNodeIDs
theNodeIDsString += theNodeID.ToString + " "
Next


Thanks,
Robin


Nov 21 '05 #1
6 1268
One way to optimize, would be to use a stringbuilder for the concatenation.
You should see a significant improvement.

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:cv*******************@news.demon.co.uk...

I have a slight bottleneck in my code, the simplicity of which leads me to
believe it is either insurmountable, or easily speeded up. I have an
ArrayList of numbers (integers, which are in fact database primary keys).
In order to pass all of these keys into a stored procedure, I generate one
long space delimited string by concatenating the IDs and pass it into SQL
Server as an NTEXT field (the stored procedure then breaks the NTEXT field
into a table and does a join with another but thats by the way). When I
have, say, 16,000 primary keys in my list, the section below can take
quite a long time (seconds!). Is this reasonable? If not, can I speed it
up any? I'm guessing the compiler is quite smart at optimization and that
something as simple as this is already more or less maxed out.

My ideas are: perhaps iterating the collection with "For Each" might be
slower than directly indexing the array. Also, perhaps I should generate
a string of the correct length and then modify it rather than relying on
the runtime to resize or re-create the string on each iteration (but
strings are immutable, right?). Perhaps there is a different way I can
convert an integer to a string that is more efficient than "ToString".

An ideas?

(theNodeIDs is an ArrayList, theNodeIDsString is a string)....

For Each theNodeID As Integer In theNodeIDs
theNodeIDsString += theNodeID.ToString + " "
Next


Thanks,
Robin

Nov 21 '05 #2
One way to optimize, would be to use a stringbuilder for the concatenation.
You should see a significant improvement.

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:cv*******************@news.demon.co.uk...

I have a slight bottleneck in my code, the simplicity of which leads me to
believe it is either insurmountable, or easily speeded up. I have an
ArrayList of numbers (integers, which are in fact database primary keys).
In order to pass all of these keys into a stored procedure, I generate one
long space delimited string by concatenating the IDs and pass it into SQL
Server as an NTEXT field (the stored procedure then breaks the NTEXT field
into a table and does a join with another but thats by the way). When I
have, say, 16,000 primary keys in my list, the section below can take
quite a long time (seconds!). Is this reasonable? If not, can I speed it
up any? I'm guessing the compiler is quite smart at optimization and that
something as simple as this is already more or less maxed out.

My ideas are: perhaps iterating the collection with "For Each" might be
slower than directly indexing the array. Also, perhaps I should generate
a string of the correct length and then modify it rather than relying on
the runtime to resize or re-create the string on each iteration (but
strings are immutable, right?). Perhaps there is a different way I can
convert an integer to a string that is more efficient than "ToString".

An ideas?

(theNodeIDs is an ArrayList, theNodeIDsString is a string)....

For Each theNodeID As Integer In theNodeIDs
theNodeIDsString += theNodeID.ToString + " "
Next


Thanks,
Robin

Nov 21 '05 #3
Robin,

"Robin Tucker" <id*************************@reallyidont.com> schrieb:
I have a slight bottleneck in my code, the simplicity of which leads me to
believe it is either insurmountable, or easily speeded up. I have an
ArrayList of numbers (integers, which are in fact database primary keys).
In order to pass all of these keys into a stored procedure, I generate one
long space delimited string by concatenating the IDs and pass it into SQL
Server as an NTEXT field (the stored procedure then breaks the NTEXT field
into a table and does a join with another but thats by the way). When I
have, say, 16,000 primary keys in my list, the section below can take
quite a long time (seconds!). Is this reasonable? If not, can I speed it
up any? I'm guessing the compiler is quite smart at optimization and that
something as simple as this is already more or less maxed out.

My ideas are: perhaps iterating the collection with "For Each" might be
slower than directly indexing the array.
The difference is marginal.
Also, perhaps I should generate a string of the correct length and then
modify it rather than relying on the runtime to resize or re-create the
string on each iteration (but strings are immutable, right?).
That's the bottleneck. Strings are immutable and your code will create new
string objects several times. Instead of using a string, use a
'StringBuilder' instead:
For Each theNodeID As Integer In theNodeIDs
theNodeIDsString += theNodeID.ToString + " "
Next


\\\
Dim sb As New StringBuilder()
For Each NodeID As Integer In NodeIDs
sb.Append(NodeID.ToString())
sb.Append(" ")
Next NodeID
.... = sb.ToString()
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
Robin,

"Robin Tucker" <id*************************@reallyidont.com> schrieb:
I have a slight bottleneck in my code, the simplicity of which leads me to
believe it is either insurmountable, or easily speeded up. I have an
ArrayList of numbers (integers, which are in fact database primary keys).
In order to pass all of these keys into a stored procedure, I generate one
long space delimited string by concatenating the IDs and pass it into SQL
Server as an NTEXT field (the stored procedure then breaks the NTEXT field
into a table and does a join with another but thats by the way). When I
have, say, 16,000 primary keys in my list, the section below can take
quite a long time (seconds!). Is this reasonable? If not, can I speed it
up any? I'm guessing the compiler is quite smart at optimization and that
something as simple as this is already more or less maxed out.

My ideas are: perhaps iterating the collection with "For Each" might be
slower than directly indexing the array.
The difference is marginal.
Also, perhaps I should generate a string of the correct length and then
modify it rather than relying on the runtime to resize or re-create the
string on each iteration (but strings are immutable, right?).
That's the bottleneck. Strings are immutable and your code will create new
string objects several times. Instead of using a string, use a
'StringBuilder' instead:
For Each theNodeID As Integer In theNodeIDs
theNodeIDsString += theNodeID.ToString + " "
Next


\\\
Dim sb As New StringBuilder()
For Each NodeID As Integer In NodeIDs
sb.Append(NodeID.ToString())
sb.Append(" ")
Next NodeID
.... = sb.ToString()
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5
Thanks both for the above. The operation was taking ~15 seconds but is now
to all intents and purposes instantaneous :))

Robin

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Robin,

"Robin Tucker" <id*************************@reallyidont.com> schrieb:
I have a slight bottleneck in my code, the simplicity of which leads me
to believe it is either insurmountable, or easily speeded up. I have an
ArrayList of numbers (integers, which are in fact database primary keys).
In order to pass all of these keys into a stored procedure, I generate
one long space delimited string by concatenating the IDs and pass it into
SQL Server as an NTEXT field (the stored procedure then breaks the NTEXT
field into a table and does a join with another but thats by the way).
When I have, say, 16,000 primary keys in my list, the section below can
take quite a long time (seconds!). Is this reasonable? If not, can I
speed it up any? I'm guessing the compiler is quite smart at optimization
and that something as simple as this is already more or less maxed out.

My ideas are: perhaps iterating the collection with "For Each" might be
slower than directly indexing the array.


The difference is marginal.
Also, perhaps I should generate a string of the correct length and then
modify it rather than relying on the runtime to resize or re-create the
string on each iteration (but strings are immutable, right?).


That's the bottleneck. Strings are immutable and your code will create
new string objects several times. Instead of using a string, use a
'StringBuilder' instead:
For Each theNodeID As Integer In theNodeIDs
theNodeIDsString += theNodeID.ToString + " "
Next


\\\
Dim sb As New StringBuilder()
For Each NodeID As Integer In NodeIDs
sb.Append(NodeID.ToString())
sb.Append(" ")
Next NodeID
... = sb.ToString()
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #6
Thanks both for the above. The operation was taking ~15 seconds but is now
to all intents and purposes instantaneous :))

Robin

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Robin,

"Robin Tucker" <id*************************@reallyidont.com> schrieb:
I have a slight bottleneck in my code, the simplicity of which leads me
to believe it is either insurmountable, or easily speeded up. I have an
ArrayList of numbers (integers, which are in fact database primary keys).
In order to pass all of these keys into a stored procedure, I generate
one long space delimited string by concatenating the IDs and pass it into
SQL Server as an NTEXT field (the stored procedure then breaks the NTEXT
field into a table and does a join with another but thats by the way).
When I have, say, 16,000 primary keys in my list, the section below can
take quite a long time (seconds!). Is this reasonable? If not, can I
speed it up any? I'm guessing the compiler is quite smart at optimization
and that something as simple as this is already more or less maxed out.

My ideas are: perhaps iterating the collection with "For Each" might be
slower than directly indexing the array.


The difference is marginal.
Also, perhaps I should generate a string of the correct length and then
modify it rather than relying on the runtime to resize or re-create the
string on each iteration (but strings are immutable, right?).


That's the bottleneck. Strings are immutable and your code will create
new string objects several times. Instead of using a string, use a
'StringBuilder' instead:
For Each theNodeID As Integer In theNodeIDs
theNodeIDsString += theNodeID.ToString + " "
Next


\\\
Dim sb As New StringBuilder()
For Each NodeID As Integer In NodeIDs
sb.Append(NodeID.ToString())
sb.Append(" ")
Next NodeID
... = sb.ToString()
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #7

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

Similar topics

6
2559
by: Kyle | last post by:
Hello all! I'm looking into developing a biological program for modeling and sequencing DNA and other biological processes. The program's main focus would be to look for similarities and...
0
1777
by: Andreas Falck | last post by:
Hi, I ran the code below on two different versions, 4.0.14 and 4.0.16 respectively, both running RH 7.3 on intel. In version mysql server version 4.0.14 the SELECT privelege suffices for...
6
1520
by: Silly | last post by:
byte Name = new byte; uint len = (uint)Name.Length; uint err = MyFunction(devID, out Name, out len); When this code is run in release build with optimize code set to true, len is evaluated to...
3
2808
by: Reddy | last post by:
The sql query for my datagrid returns 100, 000 records. But the datagrid should display 20 records per page. I am using datagrid paging, but it is taking too much time for the page to load. Is...
24
1934
by: Arne Demmers | last post by:
Hi, As a project of mine I have to do some C programming, which in fact isn't the real problem. The problem it self is that I will have to write some code that is as minimal as possible, and as...
6
1265
by: Steeve | last post by:
Hi, Is this possible to optimize this method ? It's the method than I call most offen in my application (result of Sampling Profiling in Visual Studio Performance report) I use this method to...
15
2499
by: kenneth | last post by:
I was trying to use multiple thread to optimize my following code, but met some problems, anyone can help me? k are initialized. int computePot() { int i, j; for( i=0; i<500; i++ ) { for(...
25
3754
by: jwrweatherley | last post by:
I'm pretty new to python, but am very happy with it. As well as using it at work I've been using it to solve various puzzles on the Project Euler site - http://projecteuler.net. So far it has not...
2
2067
by: pavanip | last post by:
Hi, I have an application like Optimize System Performance by using Memory speed, cpu speed and Disk speed. How to optimize memory speed,disk optimization and CPU optimization. Please provide me...
0
7099
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
6964
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
7123
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
7175
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...
1
6842
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...
0
7319
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.