0

コンマで区切られた約39000のintを含むファイルがあり、各行に13のintが含まれているため、ファイルリーダーとスキャナーを設定して読み取りと解析を行いますが、実行には文字通り1時間以上かかります。おそらく bufferedreader を使用する必要があると思いますが、実装方法がわかりません。これを行うためのより良い(より速い)方法を提案できる人はいますか?

これが私のコードです:

public class ECGFilereader { // reads the ecg files from the SD card 

public final static int numChannels = 12;   // the data is stored in 12 channels, one for each lead
public final static int numSamples = 3000; //500 = fs so *6 for 6 seconds of data
private File file;
private Scanner scanner;
short [] [] ecg = new short [numChannels] [numSamples]; //Creates a short called ecg in which all of the samples for each channel lead will be stored

 public ECGFilereader (String fname) throws FileNotFoundException 
 {
    file = new File(Environment.getExternalStorageDirectory() +"/1009856.txt");     //accesses the ecg file from the SD card

    scanner = new Scanner(file);

    scanner.useDelimiter(",|\\r\\n");   //sets commas and end's of lines as separators between each int
 }

public boolean ReadFile(Waveform[] waves) // sorts data into and array of an array (12 channels each containing 3000 samples)
{
    for (int sample=0; sample<numSamples && scanner.hasNextInt(); sample++)     //
    {
        scanner.nextInt();
        for (int chan = 0; chan<numChannels; chan++)
        {
            if(scanner.hasNextInt())
                ecg [chan] [sample] = (short) scanner.nextInt();        

            else if (scanner.hasNextLine())
            {   scanner.nextLine();
            }
            else return false;
        }   
    }
    for (int chan=0; chan<numChannels; chan++)
        waves[chan].setSignal(ecg[chan]); // sets a signal equal to the ecg array of samples for each channel
    return true;

}
}

編集:

スキャナー クラスを完全に削除したので、次のコードで完全に動作します。

public boolean ReadFile(Waveform[] waves) // sorts data into and array of an array (12 channels each containing 3000 samples)
{
try {
    BufferedReader in = new BufferedReader(new FileReader(file));

    String reader = "";
    for (int sample=0; sample<numSamples; sample++){
    if ((reader = in.readLine()) == null) {
        break;}
    else {
        String[] RowData = reader.split(","); // sets the commas as separators for each int.
        for (int chan=0; chan <12 && chan<RowData.length; chan++)
            ecg [chan][sample]= Integer.parseInt(RowData[chan+1]); //parses each int from the current row into each channel for the ecg[]
        }
    }
    in.close();
} catch (IOException e) {
    }
    for (int chan=0; chan<numChannels; chan++)
        waves[chan].setSignal(ecg[chan]); // sets a signal equal to the ECG array of samples for each channel
    return true;

}

}

4

1 に答える 1

1

あなたの遅延は、あなたが入れた最後の setSignal ループではなく、スキャナから来ていると確信していますか?

TraceViewを使用して、プログラムがハングアップしている場所を正確に把握することをお勧めします。同様の問題で私を大いに助けました。

これが役立つかどうか教えてください。最初のループの最初と最後に開始点と終了点を置くことをお勧めします。このような:

public class ECGFilereader { // SD カードから ecg ファイルを読み取ります

public final static int numChannels = 12; // データは 12 のチャネルに格納され、リードごとに 1 つ public final static int numSamples = 3000; //500 = fs so *6 で 6 秒間のデータ private File file; プライベート スキャナー スキャナー; 短い [] [] 心電図 = 新しい短い [numChannels] [numSamples]; //各チャネル リードのすべてのサンプルが保存される ecg という short を作成します

public boolean ReadFile(Waveform[] waves) // sorts data into and array of an array (12
channels each containing 3000 samples)
{   
Debug.startMethodTracing("scanner");
for (int sample=0; sample<numSamples && scanner.hasNextInt(); sample++)     //
{
    scanner.nextInt();
    for (int chan = 0; chan<numChannels; chan++)
    {
        if(scanner.hasNextInt())
            ecg [chan] [sample] = (short) scanner.nextInt();        

        else if (scanner.hasNextLine())
            scanner.nextLine();

        else return false;
    }   
}
Debug.stopMethodTracing();

Debug.startMethodTracing("setSignal");
for (int chan=0; chan<numChannels; chan++)
    waves[chan].setSignal(ecg[chan]); 
    // sets a signal equal to the ecg array of samples for each channel
Debug.stopMethodTracing();
return true;
}

このようにして、2 つの traceView ファイルを分析し、パフォーマンスの問題がどこから来ているかを確認します。

編集:

実行していることを確認する必要があるもう 1 つのことは、外部ストレージが利用可能かどうかを確認することです。次のコードでそれを行います。

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write the media
    mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // We can only read the media
    mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;
} else {
    // Something else is wrong. It may be one of many other states, but all we need
    //  to know is we can neither read nor write
    mExternalStorageAvailable = mExternalStorageWriteable = false;
}
于 2011-08-24T14:24:16.990 に答える