Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading one character at a time from a large string

I have a big string having at most 100000 character. Instead of using string.charAt[index] to read a character from the string, I converted that string into char array using string.toCharArray() method and now i am working with charArray[index]. which takes less time than string.charAt[index] method. However i want to know that, is there any other way which is faster than string.toCharArray(); method?

like image 748
Ravi Joshi Avatar asked Mar 24 '12 11:03

Ravi Joshi


1 Answers

I do not think there is a faster way. But please correct me!

A String instance is backed by a char array. charAt() does some index checks which may be the cause for it being slower than working with the array returned by toCharArray(). toCharArray() simply does a System.arraycopy() of the backing array.

like image 125
nansen Avatar answered Sep 28 '22 07:09

nansen