Re: Finding substring in character array
- From: Tom Anderson <twic@xxxxxxxxxxxxxxx>
- Date: Fri, 19 Sep 2008 00:34:12 +0100
On Thu, 18 Sep 2008, Roedy Green wrote:
On Thu, 18 Sep 2008 15:21:28 +0200, Hakan <H.L@xxxxxxxxxxxx> wrote,
quoted or indirectly quoted someone who said :
The problem is that some of the original files are really large. The runtime system reaches a state where it's dry of heap space when allocating a String for that kind of text. It works with a character array instead.
Why would a char[] take up significantly less space? A string is just a char[] with a few extra fields.
I'm guessing his problem is in allocating it - the trouble with a string is that there's no way to make one without already having all the characters in an array (or other container) which then gets copied into the string. If you have 1 MB of RAM, the biggest string you can make is 512 kB, whereas you could fill all 1 MB with a char[]. Roughly speaking, of course.
I think the real solution to the OP's problem is not to read the whole file into a char[]. Either use a streaming parser, or memory-map the file.
If he insists on searching a char[], i would suggest he reads up on the Boyer-Moore string searching algorithm. It's not that complicated, and is pretty much the fastest possible way to search his array. Faster than String.indexOf, which i believe uses a naive search.
tom
--
got EXPERTISE in BADASS BRAIN FREEZE
.
- References:
- Finding substring in character array
- From: Hakan
- Re: Finding substring in character array
- From: Roedy Green
- Finding substring in character array
- Prev by Date: Re: Using a certain percentage of the JVM storage
- Next by Date: Re: Finding substring in character array
- Previous by thread: Re: Finding substring in character array
- Next by thread: Re: Finding substring in character array
- Index(es):
Relevant Pages
|