c++ - 从文件读取数据到链表很慢

标签 c++ list large-files

我有一个大问题,我自己创建了链表和数据结构,但是数据读取功能运行很慢。如果我尝试读取 10k 结构函数大约需要 530 毫秒:

List size: 10000
Time: 530
Delete success
Press any key to continue . . .

但是当我尝试读取 10 倍大的数据量(即 100k)时,它需要大约 44500 毫秒:

List size: 100000
Time: 44512
Delete success
Press any key to continue . . .

这是我的代码:

IQ_struct.h

#ifndef IQ_sturct_H
#define IQ_struct_H

class IQ_struct {
public:
    IQ_struct();
    void setIQ(float, float);
    float getIQ();
    float getIQx();
    float getIQy();
    ~IQ_struct();
private:
    float newX;
    float newY;
};

#endif

IQ_struct.cpp

#include "IQ_struct.h"

IQ_struct::IQ_struct(){
    newX = 0;
    newY = 0;
}

IQ_struct::~IQ_struct(){}

void IQ_struct::setIQ(float x, float y){
    newX = x;
    newY = y;
}

float IQ_struct::getIQx(){
    return newX;
}

float IQ_struct::getIQy(){
    return newY;
}

智商数据.h

#include <string>
#include "IQ_struct.h";

#ifndef IQ_data_H
#define IQ_data_H

class IQ_data{

public:
    IQ_data();
    void AddData(float, float);
    void Begin();
    void Next();
    bool End();
    IQ_struct GetData();
    void ReadFromFile(std::string,int);
    int GetSize();
    ~IQ_data();


private:
    typedef struct TNode{
        IQ_struct data;
        TNode *next;
    }* nodePtr;

    nodePtr head;
    nodePtr curr;
    nodePtr temp;
    int size;
};

#endif

智商数据.cpp

#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
#include "IQ_data.h"

using namespace std;

IQ_data::IQ_data(){
    head = NULL;
    curr = NULL;
    temp = NULL;
    size = 0;
}

void IQ_data::AddData(float x, float y){
    nodePtr n = new TNode;
    n->next = NULL;
    n->data.setIQ(x,y);

    if(head != NULL){
        curr = head;
        while(curr->next != NULL){
            curr = curr->next;
        }
        curr->next = n;
    }else{
        head = n;
    }
}


void IQ_data::Begin(){
    curr = head;
}

void IQ_data::Next(){
    curr = curr->next;
}

bool IQ_data::End(){
    return curr == NULL;
}
IQ_struct IQ_data::GetData(){
    return curr->data;
}

void IQ_data::ReadFromFile(string fileName,int a){
    float x,y;
    fstream myfile(fileName, ios_base::in);
    for(int k = 0; k < a; k++){
        myfile >> x;
        myfile >> y;
        AddData(x,y);
        size = size + 1;
    }
    myfile.close();
}

int IQ_data::GetSize(){
    return size;
}

IQ_data::~IQ_data(){
    while (head != NULL){
        curr = head;
        head = head->next;
        delete curr;
    }
    delete temp;
    delete head;
    curr = NULL;
    delete curr;
    cout << "Delete success \n";
}

main.cpp

#include <iostream>
#include <fstream>
#include <omp.h>
#include <time.h>
#include <cstdlib>
#include "IQ_data.h"

using namespace std;


int main(){
    IQ_data listas;
    clock_t init, fin;
    init = clock();
    listas.ReadFromFile("0.0013.txt",100000);
    fin = clock() - init;
    cout <<"List size: "<< listas.GetSize() << endl;
    cout <<"Time: "<<fin<<endl;
    return 0;
}

我做错了什么?主要问题是我的文件包含超过 5000K 的结构。 提前感谢您的帮助:)

最佳答案

您要添加到列表的末尾,因此每次都需要遍历整个列表,随着列表变长,这需要更长的时间。

要么添加到列表的头部,要么保留指向最后一个元素的指针,以便您可以快速插入。

关于c++ - 从文件读取数据到链表很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27036682/

相关文章:

c++ - 创建对象时得到 "undefined reference error"

c++ - libpthread 和 libc 中的 undefined reference

android - 如何在 TextView 中显示非常大的文本(几兆)?

c++ - fstream 和 wfstream 的基类

list - orgmode,引用编号列表中的项目

ruby-on-rails - 在列表中查找 Rails_Admin

list - 通过反转另一个列表来扩展一个列表的最Pythonic方法是什么?

java - 如何用Java读取一个巨大的HTML文件?

python - 对行长度未知的大文件进行二进制搜索

c++ - 如何将字符串添加到字符串数组