python - In C++, How to read one file with multiple threads? -
i reading .csv file local hard drive using vs2012 in windows 7, 64-bits, 8 core.
the file reading has 50,000+ lines , each line has 200+ attributes, read data , feed them corresponding variables time consuming. therefore, wondering if can speed multithreads, each thread reads part of file.
i've googled it, , found said that, since hard drive not multithreading, using multiple threads acctually slow down. is true?
if possible read file multiple threads, can give me an example can learn from?
also, possible explicitly assign thread or task cpu core?
and final question: i've read same file python, , has been finished in few seconds. may know why python read faster c++?
reading file requires making syscall in language or os, means making call underlying operating system, , waiting put contents of file memory (assuming pass os's security checks , that). multi-threading file read indeed slow down since you'd making more syscalls pops out of program execution , hands control on operating system.
as such, best suggestion hyde's - perhaps split out parsing of file multiple threads if need be. if you're able parse file large in matter of few seconds though, i'd it's not worth it. example, if you're running graphical application, want keep separate thread file loading don't freeze ui.
on matter of speed, i'd guess there's 2 primary issues. firstly, suspect python reads it's files through memory buffer default, speed execution. if can buffer file reads (so can make fewer syscalls), might see performance gains. other issue data structures you're using in python , c++ load/parse data. without knowing code, can't suggest specific, taking little time research/think different data structures applicable program might useful. keep in mind python's , c++'s data structures have different performance profiles, 1 works in python may worse choice in c++.
edit: simple sample of using file buffering in c++ stl http://www.cplusplus.com/reference/
// read file buffer - sgetn() example #include <iostream> // std::cout, std::streambuf, std::streamsize #include <fstream> // std::ifstream int main () { char* contents; std::ifstream istr ("test.txt"); if (istr) { std::streambuf * pbuf = istr.rdbuf(); std::streamsize size = pbuf->pubseekoff(0,istr.end); pbuf->pubseekoff(0,istr.beg); // rewind contents = new char [size]; pbuf->sgetn (contents,size); istr.close(); std::cout.write (contents,size); } return 0; }
Comments
Post a Comment