Han Dong <saviola7...@gmail.com> wrote on 05/10/2010 07:18:41 PM:
> Currently I'm trying to read a text file and save it in a buffer. Below
is
> my implementation.
>
> try{
> val fp = new File(this.fileName), fr = fp.openRead();
> while(true)
> {
> s += Marshal.LINE.read(fr);
> }
> }
> catch(Exception)
> {
> //EOF
> }
>
> When the file I'm reading is about 4 mb, it seems to take a really long
> time. Is there a more optimized way of reading text files? I had no
problem
> reading the file with fread in C and was wondering if there's anything
like
> that in x10.
>
Hi,
The X10 I/O library is fairly primitive, but depending on exactly
what you want as the final result, there are probably better ways to write
this. In particular, by doing s += for each line, you are going to do O
(N^2) work to read the file (assuming s is as String, which I'm guessing it
is?).
If you want the whole file in a single buffer, then you might be
better using an x10.util.ValRailBuilder or a StringBuilder to accumulate
the result. They'll internally grow the backing storage so as to get an
amortized O(N).
If you want the data as a collection of Strings (one per line), then
a GrowableRail[String] is probably the right thing to use (in your loop
above, change s to be a GrowableRail[String] and the call add instead of +=
for each line as you read it).
Hope that helps some,
--dave
------------------------------------------------------------------------------
_______________________________________________
X10-users mailing list
X10-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/x10-users