How to use Java libgdx to effectively read text files of floating point numbers?

I am writing a small scientific data visualization application in Java / libgdx The application first reads the text file describing the 4D array:

188 225 3 6 0.001 -0.023 1.345 2.475 ... 4.327

The first four integers specify the dimension of the array: 188 x 255 x 3 x 6, followed by 188 x 255 x 3 x 6 ~ = 760000 numbers In general, text files occupy about 5MB of space

The 4D array represents an "enhanced" image 188 pixels high and 255 pixels wide with three color channels (RGB) Each pixel channel has 6 "parameters" instead of one in a conventional RGB image

I use the following method to copy from the official document to read a space delimited list into memory:

FileHandle ptmFile = Gdx.files.internal(filename);
String ptmText= ptmFile.readString();

Then I created a simple wrapper class, augpixel, which is actually a wrapper for an array of 6 elements per pixel I use the scanner to read and parse every number in the string (please forgive my semi pseudo code):

Scanner scan = new Scanner(ptmText);
int nRows = scan.nextInt();
int nCols = scan.nextInt();
int nColors = scan.nextInt();
int nParams = scan.nextInt();


AugPixel[][][] im = new AugPixel[nRows][nCols][nColors];

for (i = 1~nRows)
  for (j = 1~nCols)
    for (k = 1~nColors)
      im[i][j][k] = new AugPixel(nParams);
      for (m = 1~nParams) 
        im[i][j][k].addParam(scan.nextFloat());

This method is applicable to desktop version It takes about 5 seconds to load the file and build all the augpixel objects However, when I move the application to the actual Android device (Nexus 7 32g (2nd generation)), the loading process takes 5 minutes!

So my question is: is there any way to speed up the loading process? I am free to redefine the organization of files

thank you!

Solution

If reading data from a file is the main bottleneck, you can try some form of asynchronous IO so that some strings can be processed when the rest is loaded

If the main bottleneck is parsing strings, you may try multithreading or switching to binary file format in some way If you are willing to give up text format, the best solution may be to switch to binary file format, which should greatly reduce processing time and file size

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>