Home > Software engineering >  How to concatenate millions array records to a single string faster?
How to concatenate millions array records to a single string faster?

Time:08-24

I am trying to concatenate all values of a 2D array to a single String. But performance is really impacted by size of the array (2800 X 1700). I there a better way to do it ?

Thank you !

float[][] datas = obj.getMatrix();
String result = "";
int[] shape = {2800,1700};
for(k=0; k<shape[0]; k  ){
   for(j=0; j<shape[1]; j  ){
      result  = (datas[k][j]   "").getBytes();
   }
}

UPDATE

I am using unidata netCDF java API to convert my data for ncML response.

My float[][] gives me some values that i want to add to a list.

Instead of doing a String result = "10.0,15.1,45.6,25.4....." I want to convert each value to byte[]. My string will look like result = "[B@xxxxxxxx[B@xxxxxxxx[B@xxxxxxxx[B@xxxxxxxx...."

Then, I can convert my result into Base64 using Base64.getEncoder().encode(result.getBytes())

It will look like :

float[][] datas = obj.getMatrix();
    StringBuffer result = new StringBuffer();
    int[] shape = {2800,1700};
    for(k=0; k<shape[0]; k  ){
       for(j=0; j<shape[1]; j  ){
          result.append((datas[k][j]   "").getBytes());
       }
    }
byte[] encodedByte = Base64.getEcnoder().encode(result);
String encodedStr = new String(result);

Then, when user get the response, he will decode the result, obtain the "[B@xxxxxxxx[B@xxxxxxxx[B@xxxxxxxx[B@xxxxxxxx...." responseand can identify values

UPDATE 2

Thanks to all the comments, I understand what it cannot works. Finally, I decided to just do : result.append(datas[k][j] ",");

CodePudding user response:

Use StringBuilder instead of string for result. This will reduce time to few secs. as given below

StringBuilder result = new StringBuilder();
        int[] shape = {2800,1700};
        for(int k=0; k<shape[0]; k  ){
            for(int j=0; j<shape[1]; j  ){
                result.append ((datas[k][j]   "").getBytes() );
            }
        }
  • Related