C++快速讀取大文件

debug的時候須要等很長時間讀模型,查資料發現了兩種快速讀取大文件的方法。ios

test 1:每次讀一個字符串spa

test 二、3一次讀取整個文件debug

    {//test 1
        string buf;
        clock_t start = clock();
        ifstream fin(objpath);
        
        while (fin >> buf);
        
        fin.close();
        clock_t end = clock();
        cout << "time : " << ((double)end - start) / CLOCKS_PER_SEC << "s\n";
    }
    {//test 2
        clock_t start = clock();
        ifstream fin(objpath, std::ios::binary);
        
        vector<char> buf(fin.seekg(0, std::ios::end).tellg());
        fin.seekg(0, std::ios::beg).read(&buf[0], static_cast<std::streamsize>(buf.size()));
        
        fin.close();
        clock_t end = clock();
        cout << "time : " << ((double)end - start) / CLOCKS_PER_SEC << "s\n";
    }
    {//test 3
        clock_t start = clock();
        ifstream fin(objpath);

        stringstream buf;
        buf << fin.rdbuf();

        fin.close();
        clock_t end = clock();
        cout << "time : " << ((double)end - start) / CLOCKS_PER_SEC << "s\n";
    }

文件大小爲112M,花費的時間分別爲:code

time : 36.809s
time : 0.092s
time : 4.213s

因而將loader改爲了第二種。blog

相關文章
相關標籤/搜索