C++快速读取大文件


debug的时候需要等很长时间读模型,查资料发现了两种快速读取大文件的方法。

test 1:每次读一个字符串

test 2、3一次读取整个文件

    {//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,花费的时间分别为:

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

于是将loader改成了第二种。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM