我的代码合并了两个android传感器数据double [] []数组,然后连接成一个文件存储字符串。合并代码对齐时间戳并且工作正常,但字符串连接对于大型数据阵列(例如30,000条记录)来说非常慢。有没有更好的方法来将double [] []数组存储为文件中的字符串数据?在性能方面,是否有更好的创建字符串对象的技术?性能问题出现在下面的for循环的最后一组中。
package com.xxxx.android.sara.dva;
import java.lang.reflect.Array;
public final class CombineDblArr {
public double[][] rtrnArr;
public String bigFileStrng;
CombineDblArr(double[][] lrgArr, int lrgTSDim, double[][] smlArr,
int smlTSDim, double startTS) {
// find the closest TSes for each array
// pull the data from the arrays into a file loading string
// search the arrays for the first timestamp that is nearest the
// start TS
double d = -1;
double bestDistanceNow = Double.MAX_VALUE;
int locLrgDex = 0;
int locSmlDex = 0;
int lrgSize = lrgArr.length;
for (int i = 0; i < lrgSize; i++) {
if (lrgArr[i][lrgTSDim] == startTS) {
locLrgDex = i;
break;
} else {
d = Math.abs(lrgArr[i][lrgTSDim] - startTS);
// cannot optimize the search due to cyclical TS design of
// arrays
// the max TS delta could be in consecutive rows
if (d < bestDistanceNow) {
bestDistanceNow = d;
locLrgDex = i;
}
}
}
d = -1;
bestDistanceNow = Double.MAX_VALUE;
int smlSize = smlArr.length;
for (int i = 0; i < smlSize; i++) {
if (smlArr[i][smlTSDim] == startTS) {
locSmlDex = i;
break;
} else {
d = Math.abs(smlArr[i][smlTSDim] - startTS);
// cannot optimize the search due to cyclical TS design of
// arrays
// the max TS delta could be in consecutive rows
if (d < bestDistanceNow) {
bestDistanceNow = d;
locSmlDex = i;
}
}
}
// get the dims of the 2D arrays
int lrgDmsn = 0;
int smlDmsn = 0;
try {
lrgDmsn = lrgArr[0].length;
smlDmsn = smlArr[0].length;
} catch (ArrayIndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rtrnArr = new double[lrgSize][lrgDmsn + smlDmsn];
for (int j = 0; j < lrgSize; j++) {
// align the TS when combining rows. both arrays are the same size,
// but diff TS values
for (int k = 0; k < lrgDmsn; k++) {
rtrnArr[j][k] = lrgArr[locLrgDex][k];
}
for (int k = 0; k < smlDmsn; k++) {
rtrnArr[j][k + lrgDmsn - 1] = smlArr[locSmlDex][k];
}
// see if the next TS in smlArr is within range of the next TS in
// lrgArr
locLrgDex++;
// check for rollover condition
if ((locLrgDex + 1) >= lrgSize)
locLrgDex = 0;
if (smlArr[locSmlDex + 1][smlTSDim] < lrgArr[locLrgDex + 1][lrgTSDim])
locSmlDex++;
if (locSmlDex >= lrgSize)
locSmlDex = 0;
}
// create the writeable string
// this code is incredibly slow
int bigFileDim = lrgDmsn + smlDmsn;
bigFileStrng = "";
for(int l=0; l<lrgSize;l++){
// break if a TS value is 0
if(rtrnArr[l][lrgTSDim] != 0.0){
// build the string
for (int m = 0; m < bigFileDim; m++)
bigFileStrng = bigFileStrng + (Double.toString(rtrnArr[l][m])) + ",";
bigFileStrng = bigFileStrng + "\n";
}else
break;
}
return;
}
}