473,614 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ 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, theNodeIDsStrin g is a string)....

For Each theNodeID As Integer In theNodeIDs
theNodeIDsStrin g += theNodeID.ToStr ing + " "
Next


Thanks,
Robin


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

"Robin Tucker" <id************ *************@r eallyidont.com> wrote in
message news:cv******** ***********@new s.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, theNodeIDsStrin g is a string)....

For Each theNodeID As Integer In theNodeIDs
theNodeIDsStrin g += theNodeID.ToStr ing + " "
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************ *************@r eallyidont.com> wrote in
message news:cv******** ***********@new s.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, theNodeIDsStrin g is a string)....

For Each theNodeID As Integer In theNodeIDs
theNodeIDsStrin g += theNodeID.ToStr ing + " "
Next


Thanks,
Robin

Nov 21 '05 #3
Robin,

"Robin Tucker" <id************ *************@r eallyidont.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
theNodeIDsStrin g += theNodeID.ToStr ing + " "
Next


\\\
Dim sb As New StringBuilder()
For Each NodeID As Integer In NodeIDs
sb.Append(NodeI D.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************ *************@r eallyidont.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
theNodeIDsStrin g += theNodeID.ToStr ing + " "
Next


\\\
Dim sb As New StringBuilder()
For Each NodeID As Integer In NodeIDs
sb.Append(NodeI D.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******** ********@TK2MSF TNGP09.phx.gbl. ..
Robin,

"Robin Tucker" <id************ *************@r eallyidont.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
theNodeIDsStrin g += theNodeID.ToStr ing + " "
Next


\\\
Dim sb As New StringBuilder()
For Each NodeID As Integer In NodeIDs
sb.Append(NodeI D.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******** ********@TK2MSF TNGP09.phx.gbl. ..
Robin,

"Robin Tucker" <id************ *************@r eallyidont.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
theNodeIDsStrin g += theNodeID.ToStr ing + " "
Next


\\\
Dim sb As New StringBuilder()
For Each NodeID As Integer In NodeIDs
sb.Append(NodeI D.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
2572
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 differences between different models of experiments. I would also being using information from this website in my work also: http://www.ncbi.nlm.nih.gov/
0
1787
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 "optimize table", but (seemingly) the server version 4.0.16 requires INSERT as well. Is the INSERT privelege necessary for performing optimize in mysql
6
1527
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 0. If it is run with optimize code set to false, len is evaluated as 256 (what i want). If I add an extra line of code after declaring len, like:
3
2817
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 there any way I can optimize the speed. Any sample code would be great. Thanks, Reddy
24
1955
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 fast as possible. Are there any gidelines for this kind of thing? How do I know if my code is possibly the smallest and fastest I could write for a certain algo?? And where can I find thise kind of guidelines???
6
1281
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 find a particular Node in a List<Node>. (Note: JobNumber is a int) public bool NodeMachineJobPredicate(Node N)
15
2514
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( j=0; j<i-1; j++ ) {
25
3776
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 let me down, but it has proved surprisingly slow on one puzzle. The puzzle is: p is the perimeter of a right angle triangle with integral length sides, {a,b,c}. which value of p < 1000, is the number of solutions {a,b,c} maximised? Here's my...
2
2081
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 some sample source code to optimize system performance. Thanks pavani
0
8179
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8124
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8427
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6087
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5538
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4119
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1712
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1421
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.