473,467 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Performance of Split vs. Substring and IndexOf?

Hi!

If I want to get the first element of a delimited string, which of this two
methods would be the best performancevise?

Dim s0 as String = "abc;def;ggg"
Dim s1 as String
Dim s2 as String

Method 1:
s1 = s0.Split(";")(0)

Method 2:
s2 = s0.Substring(0, s0.IndexOf(";"))
Brgds

Jonas

Nov 20 '05 #1
9 9763
"Jonas" <jo***@nospam.pl> schrieb

If I want to get the first element of a delimited string, which of
this two methods would be the best performancevise?
Why not test it?
Dim s0 as String = "abc;def;ggg"
Dim s1 as String
Dim s2 as String

Method 1:
s1 = s0.Split(";")(0)

Method 2:
s2 = s0.Substring(0, s0.IndexOf(";"))

I guess Method2 is faster because only the first part is extracted from the
string. You can also try to pass a char instead of a string to IndexOf:

s2 = s0.Substring(0, s0.IndexOf(";"c)) ' ";"c for char literals
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
Cor
With this short string?
Dim s0 as String = "abc;def;ggg"


Do you want to know it in 1/10000000000000000000000000 second?

Cor

Nov 20 '05 #3
Cor
Hi Armin,

I had the same answer but changed it (even with the character).

See my answer.

Cor
Nov 20 '05 #4
"Cor" <no*@non.com> schrieb
Hi Armin,

I had the same answer but changed it (even with the character).

See my answer.


What if it's executed several thousand times in a loop?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #5
Cor
Hi Armin,

Last week I did a test about this again with the Regex in it, I don't know
if you have seen it.

The processing of the loop did take the most time.

So I think that this kind of things are only a question when it is a long
string, and then it could not be another answer than "just take the 4
characters with the substring" of course.

Cor
Nov 20 '05 #6
"Cor" <no*@non.com> schrieb

Last week I did a test about this again with the Regex in it, I don't
know if you have seen it.
No, I haven't followed the Regex threads.
The processing of the loop did take the most time.

So I think that this kind of things are only a question when it is a
long string, and then it could not be another answer than "just take
the 4 characters with the substring" of course.


You are right, but whenever I have the choice, I choose the faster version.
:-)
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
Nov 20 '05 #7
in addition

are you going to use the other parts of the string?
if so use the split and send it to a string array

eric

"Jonas" <jo***@nospam.pl> wrote in message
news:uh**************@TK2MSFTNGP12.phx.gbl...
Hi!

If I want to get the first element of a delimited string, which of this two methods would be the best performancevise?

Dim s0 as String = "abc;def;ggg"
Dim s1 as String
Dim s2 as String

Method 1:
s1 = s0.Split(";")(0)

Method 2:
s2 = s0.Substring(0, s0.IndexOf(";"))
Brgds

Jonas

Nov 20 '05 #8
:-)

It was just an example, the actual string is longer, up to 256 characters.

/Jonas

"Cor" <no*@non.com> wrote in message
news:eV**************@TK2MSFTNGP10.phx.gbl...
With this short string?
Dim s0 as String = "abc;def;ggg"


Do you want to know it in 1/10000000000000000000000000 second?

Cor

Nov 20 '05 #9
Cor
Hi Jonas,

I think you saw my answer with Armin, it is the same as he, but actualy with
strings of 256characters the difference is also almost nothing (on a modern
computer).

But I would choose for your option without the split (when it is not
something Eric said) and then with the small c from character in it as Armin
made in his example.

Cor
Nov 20 '05 #10

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

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
7
by: Gui Lloyd | last post by:
I have a problem with performance in IE. The script above works quite fine when the table has a small number of elements, but, when the table has 2500 elements, when I click in the checkbox of the...
8
by: Geoff Cox | last post by:
Hello, If I have fred = "0 -10 5 6 "; how can I split above into groups of 2, ie 0,-10
8
by: mannyGonzales | last post by:
Hey guys, Earliery I posted this common task of reading a csv file. My data read as: "1","2","3" Unfortunately it now reads as: "1","Text with, comma", "2" embedded commas!...
4
by: Roshawn | last post by:
Hi, I am retrieving a list of book titles from a web service. What I'd like to do is shorten the titles, if possible. For example, there is a book titled "Malicious Mobile Code: Virus...
2
by: mallard134 | last post by:
Could someone please help a newbee vb programmer with a question that is driving me crazy. I am trying to understand a line of code that is supposed to return the domain portion of a valid email...
2
by: Digital Fart | last post by:
following code would split a string "a != b" into 2 strings "a" and "b". but is there a way to know what seperator was used? string charSeparators = { "=", ">=", "<=" , "!=" }; string s1 =...
8
by: Ben Dewey | last post by:
Anyone, I am trying to do a string replace of a custom Html Tag that is Case Insensitive and Fast, I will be calling this function a bunch of times. Any thoughts about using maybe a...
1
Atli
by: Atli | last post by:
The following small HowTo is a compilation of an original problem in getting some cookie-values through different methods of string-handling. The original Problem was posted as follows: As...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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
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...
0
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
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 ...

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.