使用扫描器类来解析文件,但运行时间太长

发布于 2024-12-01 07:15:38 字数 2723 浏览 0 评论 0原文

我有一个包含大约 39000 个整数的文件,每个整数由逗号分隔,每行有 13 个整数,因此我设置了一个文件读取器和一个扫描仪来读取和解析它,但是它运行起来大约需要一个多小时。我想我可能需要使用 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;

}

}

I have a file containing around 39000 ints seperated by commas with 13 ints on each line so i set up a file reader and a scanner to read and parse it however it litterally takes over an hour to run. I think I probably need to use a bufferedreader but not sure how to implement it. Can anybody suggest a better (faster) way of doing this?

Here's my code:

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;

}
}

EDIT:

I have now removed the scanner class completely and it works perfectly with the following code:

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;

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我爱人 2024-12-08 07:15:38

您确定您的延迟来自扫描仪而不是您放入的最终 setSignal 循环吗?

我建议使用 TraceView 来准确找出你的程序在哪里挂了。它对我解决类似问题有很大帮助。

让我知道这是否有帮助。我建议将开始点和停止点放在第一个循环的开始和结束处。像这样:

public class ECGFilereader { // 从 SD 卡读取心电图文件

public Final static int numChannels = 12; // 数据存储在 12 个通道中,每个通道对应一个通道
公共最终静态 int numSamples = 3000; //500 = fs 所以 *6 表示 6 秒的数据
私人文件文件;
私人扫描仪扫描仪;
短[][]心电图=新短[numChannels][numSamples]; //创建一个名为 ecg 的简短内容,其中将存储每个通道导联的所有样本。

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 文件进行分析,以了解性能问题来自何处。

编辑:

您需要确保正在做的另一件事是检查外部存储是否可用。使用以下代码执行此操作:

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;
}

Are you sure that your delay is coming from the Scanner and not the final setSignal loop you put in?

I would recommend using TraceView to figure out exactly where your program is hanging up. It helped me greatly with a similar problem.

Let me know if this helps. I suggest putting the start and stop points at the beginning and end of the first loop. Like this:

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 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;
}

This way you will have 2 traceView files to analyze to see where your performance issues are coming from.

EDIT:

One other thing you need to make sure you're doing is checking if the external storage is available. Do that with the following code:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文