java - Reading file takes too long -


my application starts parsing ~100mb file sd card , takes minutes so. put in perspective, on pc, parsing same file takes seconds.

i started naively implementing parser using matcher , pattern, ddms told me 90% of time spent computing regular expression. , took more half hour parse file. pattern ridiculously simple, line consists of:

id (a number) <tab> lang (a 3-to-5 character string) <tab> data (the rest) 

i decided try , use string.split. didn’t show significant improvements, because function might use regular expression itself. @ point decided rewrite parser entirely, , ended on this:

protected collection<sentence> doinbackground( void... params ) {     bufferedreader reader = new bufferedreader( new filereader( sentencefile ) );      string currentline = null;     while ( (currentline = reader.readline()) != null ) {         treatline( currentline, allsentences );     }      reader.close();     return allsentences; }  private void treatline( string line, collection<sentence> allsentences ) {     char[] str = line.tochararray();      // ...     // treat array of chars id, language , data      allsentences.add( new sentence( id, lang, data ) ); } 

and noticed huge boost. took minutes instead of half-an-hour. wasn’t satisfied profiled , realized bottleneck bufferedreader.readline. wondered: io-bound, lot of time taken filling intermediary buffer don’t need. rewrote whole thing using filereader directly:

protected collection<sentence> doinbackground( void... params ) {     filereader reader = new filereader( sentencefile );     int currentchar;     while ( (currentchar = reader.read()) != -1 ) {         // parse id         // ...                      // parse language         while ( (currentchar = reader.read()) != -1 ) {             // parsing stuff         }          // parse sentence data         while ( (currentchar = reader.read()) != -1 ) {             // parse parse parse         }          allsentences.add( new sentence( id, lang, data ) );     }      reader.close(); } 

and quite surprised realize performance super bad. of time spent in filereader.read, obviously. guess reading char costs lot.

now bit out of inspiration. tip?

another option might enhance performance use inputstreamreader around fileinputstream. you'll have buffering may increase performance. see this tutorial more information - not follow blindly. instance you're using char array can use char array buffer (and send treatline() when you've reached new-line).

yet suggestion use thread directly. documentation on asynctask says (my intonation):

asynctask designed helper class around thread , handler , not constitute generic threading framework. asynctasks should ideally used short operations (a few seconds @ most.) if need keep threads running long periods of time, highly recommended use various apis provided java.util.concurrent pacakge such executor, threadpoolexecutor , futuretask.

also, getting faster sd card - main reason being slower on desktop. normal hd can read maybe 60 mb/s , slow sd card 2 mb/s.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -