c語言中文件讀寫面試題,在C ++中有效讀取非常大的文本文件

我將對其進行重新設計以充當流式傳輸,而不是在一個塊上。

一個更簡單的方法是:

std::ifstream ifs("input.txt");

std::vector parsed(std::istream_iterator(ifs), {});

如果您大致知道期望多少個值,那么預先使用std::vector::reserve它可以進一步加快速度。

另外,您可以使用內存映射文件并遍歷字符序列。

更新 我修改了上面的程序以將uint32_ts 解析為向量。

使用4.5GiB [1] 的樣本輸入文件時,程序將在9秒 [2] 內運行:

sehe@desktop:/tmp$ make -B && sudo chrt -f 99 /usr/bin/time -f "%E elapsed, %c context switches" ./test smaller.txt

g++ -std=c++0x -Wall -pedantic -g -O2 -march=native test.cpp -o test -lboost_system -lboost_iostreams -ltcmalloc

parse success

trailing unparsed: '

'

data.size(): 402653184

0:08.96 elapsed, 6 context switches

當然,它至少分配402653184 * 4 字節= 1.5吉字節。因此,當您讀取一個45

GB的文件時,您將需要大約15GiB的RAM來存儲矢量(假設重新分配時沒有碎片):45GiB解析在45分鐘內完成10分鐘* :

make && sudo chrt -f 99 /usr/bin/time -f "%E elapsed, %c context switches" ./test 45gib_uint32s.txt

make: Nothing to be done for `all'.

tcmalloc: large alloc 17570324480 bytes == 0x2cb6000 @ 0x7ffe6b81dd9c 0x7ffe6b83dae9 0x401320 0x7ffe6af4cec5 0x40176f (nil)

Parse success

Trailing unparsed: 1 characters

Data.size(): 4026531840

Time taken by parsing: 644.64s

10:45.96 elapsed, 42 context switches

相比之下,僅運行wc -l 45gib_uint32s.txt就花費了大約12分鐘(盡管沒有實時優先級調度)。wc是 極快

完整代碼用于基準測試

#include

#include

#include

namespace qi = boost::spirit::qi;

typedef std::vector data_t;

using hrclock = std::chrono::high_resolution_clock;

int main(int argc, char** argv) {

if (argc<2) return 255;

data_t data;

data.reserve(4392580288); // for the 45 GiB file benchmark

// data.reserve(402653284); // for the 4.5 GiB file benchmark

boost::iostreams::mapped_file mmap(argv[1], boost::iostreams::mapped_file::readonly);

auto f = mmap.const_data();

auto l = f + mmap.size();

using namespace qi;

auto start_parse = hrclock::now();

bool ok = phrase_parse(f,l,int_parser() % eol, blank, data);

auto stop_time = hrclock::now();

if (ok)

std::cout << "Parse success\n";

else

std::cerr << "Parse failed at #" << std::distance(mmap.const_data(), f) << " around '" << std::string(f,f+50) << "'\n";

if (f!=l)

std::cerr << "Trailing unparsed: " << std::distance(f,l) << " characters\n";

std::cout << "Data.size(): " << data.size() << "\n";

std::cout << "Time taken by parsing: " << std::chrono::duration_cast<:chrono::milliseconds>(stop_time-start_parse).count() / 1000.0 << "s\n";

}

[1] 產生od -t u4 /dev/urandom -A none -v -w4 | pv | dd bs=1M

count=$((9*1024/2)) iflag=fullblock > smaller.txt

[2] 顯然,這與在Linux上的緩沖區緩存中緩存的文件有關–大文件沒有此好處

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/395465.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395465.shtml
英文地址,請注明出處:http://en.pswp.cn/news/395465.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

每次調試都必須clean_如何使用“ The Clean Architecture”每次編寫健壯的應用程序...

每次調試都必須cleanby Daniel Oliveira丹尼爾奧利維拉(Daniel Oliveira) 如何使用“ The Clean Architecture”每次編寫健壯的應用程序 (How to write robust apps every time, using “The Clean Architecture”) As developers, we can’t keep from using external librar…

404. Sum of Left Leaves

題目來源&#xff1a; 自我感覺難度/真實難度&#xff1a; 題意&#xff1a; 分析&#xff1a; 自己的代碼&#xff1a; class Solution(object):def sumOfLeftLeaves(self, root):""":type root: TreeNode:rtype: int"""left[]if not root:retu…

Laravel Composer 命令大全

2019獨角獸企業重金招聘Python工程師標準>>> ???????1、安裝 Laravel composer create-project --prefer-dist laravel/laravel 5.xx user-project 2、.env 文件操作 生成 APP_KEY&#xff1a;php artisan key:generate 緩存 .env 配置&#xff…

linux中initrd的含義,Linux2.6 內核的 Initrd 機制解析

1&#xff0e;什么是 Initrdinitrd 的英文含義是 boot loaderinitialized RAM disk&#xff0c;就是由 boot loader 初始化的內存盤。在 linux內核啟動前&#xff0c; boot loader 會將存儲介質中的 initrd 文件加載到內存&#xff0c;內核啟動時會在訪問真正的根文件系統前先訪…

VBS基礎篇 - 常量

VBS基礎篇 - 常量 常量&#xff1a;指的是在程序運行過程中其值保持不變的量&#xff0c;它用來保存固定不變的數值&#xff0c;字符串等常數 。 常量的定義&#xff1a;在vbscript中使用使用 Const 指令可以創建名稱具有一定含義的字符串型或數值型常量&#xff0c;并給它們賦…

為什么虛擬助手的前途一片光明

by Steve史蒂夫(Steve) 為什么虛擬助手的前途一片光明 (Why the future is bright for Virtual Assistants) I purchased my first mobile phone in the summer of 1999. I was 17 years old, growing up in a nondescript town in the Midlands of the United Kingdom. The p…

利用深度學習來預測股票價格變動

https://www.toutiao.com/i6644852565341110791/ 利用深度學習來預測股票價格變動&#xff08;長文&#xff0c;建議收藏&#xff09; 原創 不靠譜的貓 2019-01-10 21:01:39完整架構概述 在這篇文章中&#xff0c;我將創建一個預測股票價格變動的完整過程。我們將使用生成對抗網…

C語言 用鏈表對學號進行排序,求解C語言中建立一個對鏈表按照學號進行排序的問題...

功能&#xff1a;選擇排序(由小到大)返回&#xff1a;指向鏈表表頭的指針*//*選擇排序的基本思想就是反復從還未排好序的那些節點中&#xff0c;選出鍵值(就是用它排序的字段&#xff0c;我們取學號num為鍵值)最小的節點&#xff0c;依次重新組合成一個鏈表。我認為寫鏈表這類程…

字符集(CHARACTER SET)和校對集(COLLATE)

http://blog.sina.com.cn/s/blog_9707fac301016wxm.html http://www.th7.cn/db/mysql/201412/84636.shtml 從上文中可以看出character_set_connection、character_set_client、 character_set_results三個字符集什么時候用到。從實際上可以看到&#xff0c;當客戶端連接服務器的…

shell 本地接口自動化

一.基于http/https的接口 一般情況下&#xff0c;當前大多公司在做接口自動化的時候都會使用一些工具&#xff1b;比如&#xff1a;postman/jmeter/python自研開發接口平臺。。。 以上的情況&#xff0c;都是在源碼與測試使用分離的情況下實踐的。也就是說&#xff1a;目前國內…

hitchhiker部署_《 Hitchhiker的Python機器學習指南》

hitchhiker部署by Conor Dewey由Conor Dewey 《 Hitchhiker的Python機器學習指南》 (The Hitchhiker’s Guide to Machine Learning in Python) 提供實施代碼&#xff0c;教學視頻等 (Featuring implementation code, instructional videos, and more) 趨勢 (The Trend) Machi…

CAD庫中列舉所有航路點

select distinct f1.airway_point_name,f1.latitude,f1.longitude,upper(f1.airway_point_type_name)type,f2.code_fir from airway_ordered_point f1, v_airway_point f2where f2.significant_point_idf1.airway_point_idorder by code_fir, type,airway_point_name轉載于:htt…

第50次二級c語言真題,2006年4月全國計算機等級考試二級C語言筆試試卷含答案

一、選擇題((1)一(10)每題2分&#xff0c;(11)一(50)每題1分&#xff0c;共60分)下列各題A)、B)、C)、D)四個選項中&#xff0c;只有一個選項是正確的&#xff0c;請將正確選項涂寫在答題卡相應位置上&#xff0c;答在試卷上不得分。(1)下列選項中不屬于結構化程序設計方法的是…

python hashlib模塊

摘要算法簡介 Python的hashlib提供了常見的摘要算法&#xff0c;如MD5&#xff0c;SHA1等等。 什么是摘要算法呢&#xff1f;摘要算法又稱哈希算法、散列算法。它通過一個函數&#xff0c;把任意長度的數據轉換為一個長度固定的數據串&#xff08;通常用16進制的字符串表示&…

TZOJ 5101 A Game(區間DP)

描述 Consider the following two-player game played with a sequence of N positive integers (2 < N < 100) laid onto a 1 x N game board. Player 1 starts the game. The players move alternately by selecting a number from either the left or the right end o…

國家職業標準職業編碼查詢_為什么我學會編碼而不是從事金融職業

國家職業標準職業編碼查詢by Amir Ghafouri通過阿米爾加富里(Amir Ghafouri) 為什么我學會編碼而不是從事金融職業 (Why I learned to code instead of pursuing a career in finance) Last year I faced a major life and career decision: commit to pursuing a Chartered F…

go tool trace goalng調優工具

為什么80%的碼農都做不了架構師&#xff1f;>>> 你想知道你的Go程序在做什么嗎&#xff1f; go tool trace 可以向你揭示&#xff1a;Go程序運行中的所有的運行時事件。 這種工具是Go生態系統中用于診斷性能問題時&#xff08;如延遲&#xff0c;并行化和競爭異常…

程序員 文本編輯器 c語言,程序員必備的五款文本編輯器

原標題&#xff1a;程序員必備的五款文本編輯器程序員的工作離不開文本編輯器&#xff0c;有人說一個txt就能搞定&#xff0c;但txt面對如今復雜的要求&#xff0c;明顯有些捉襟見肘&#xff0c;下面推薦五款超級好用的文本編輯器及搭配軟件&#xff0c;絕對是程序員的大愛。程…

PCH文件的創建和配置

1.PCH文件的的創建 (1)CommandN (2)打開新建文件窗口:ios->other->PCH file&#xff0c;創建一個pch文件 2.PCH文件的配置 (1)在工程的TARGETS里邊Building Setting中搜索Prefix Header (2)然后在Precompile Prefix Header下邊的Prefix Header右邊雙擊&#xff0c;添加剛…

ci 數據庫異常捕獲_系統地捕獲錯誤:如何通過4個步驟構建GitLab CI測試管道

ci 數據庫異常捕獲by Joyz通過喬伊斯 系統地捕獲錯誤&#xff1a;如何通過4個步驟構建GitLab CI測試管道 (Catch bugs systematically: how to build a GitLab CI testing pipeline in 4 steps) Your first app is a hit the day it’s launched. But one week later, you rea…