Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.substring vs String[].split

I have a comma delaminated string that when calling String.split(",") it returns an array size of about 60. In a specific use case I only need to get the value of the second value that would be returned from the array. So for example "Q,BAC,233,sdf,sdf," all I want is the value of the string after the first ',' and before the second ','. The question I have for performance am I better off parsing it myself using substring or using the split method and then get the second value in the array? Any input would be appreciated. This method will get called hundreds of times a second so it's important I understand the best approach regarding performance and memory allocation.

-Duncan

like image 224
Duncan Krebs Avatar asked Dec 21 '12 21:12

Duncan Krebs


People also ask

Is substring faster than split?

When you run this multiple times, the substring wins on time hands down: 1,000,000 iterations of split take 3.36s, while 1,000,000 iterations of substring take only 0.05s.

What is difference between Split and substring in Java?

Split("|"c) , you'll get an array as {"Hello", " world"} . . substring is used to get part of the string at a starting index through a specified length. For example, to get “ello” out of “Hello| world”, you could use "Hello| world".

What is the difference between the string method slice () and substring ()?

slice() extracts parts of a string and returns the extracted parts in a new string. substr() extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters. substring() extracts parts of a string and returns the extracted parts in a new string.

Can you split a string array?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.


2 Answers

Since String.Split returns a string[], using a 60-way Split would result in about sixty needless allocations per line. Split goes through your entire string, and creates sixty new object plus the array object itself. Of these sixty one objects you keep exactly one, and let garbage collector deal with the remaining sixty.

If you are calling this in a tight loop, a substring would definitely be more efficient: it goes through the portion of your string up to the second comma ,, and then creates one new object that you keep.

String s = "quick,brown,fox,jumps,over,the,lazy,dog";
int from = s.indexOf(',');
int to = s.indexOf(',', from+1);
String brown = s.substring(from+1, to);

The above prints brown

When you run this multiple times, the substring wins on time hands down: 1,000,000 iterations of split take 3.36s, while 1,000,000 iterations of substring take only 0.05s. And that's with only eight components in the string! The difference for sixty components would be even more drastic.

like image 96
Sergey Kalinichenko Avatar answered Oct 12 '22 15:10

Sergey Kalinichenko


ofcourse why iterate through whole string, just use substring() and indexOf()

like image 26
jmj Avatar answered Oct 12 '22 15:10

jmj