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改成了第二种。

 

转载于:https://www.cnblogs.com/redips-l/p/8258306.html

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值