シングルスレッドとマルチスレッド読み込みファイル

2021 ワード

//                       
//      2-3  ,         ,              
//       10    ,         ,        
//       ,            io   ,           
#include 
#include 
#include 
#include 
#include 
using namespace std;
void ReadFileS(const char **path,int len){
	for (int i = 0; i < len;i++)
	{
		fstream fs(*(path + i), ios_base::in | ios_base::out);
		string line;
		int lineCount = 1;
		while (fs && getline(fs, line));
			//cout << "File: " << *(path + i) << "line:" << lineCount++ << ", " << line.c_str() << endl;
	}
}


void _tReadFile(const char* path){
	fstream fs(path, ios_base::in | ios_base::out);
	string line;
	int lineCount = 1;
	while (fs && getline(fs, line));
		//cout << "File: " << path << "line:" << lineCount++ << ", " << line.c_str() << endl;
}


void ReadFileM(const char **path, int len){
	for (int i = 0; i < len; i++)
	{
		std::thread t(_tReadFile,*(path+i));
		t.join();
	}
}


void main(){
	const char *path[13] = { "D:\\a.txt", "D:\\b.txt", "D:\\a.txt", "D:\\a.txt", "D:\\a.txt", "D:\\a.txt", "D:\\a.txt", "D:\\a.txt","D:\\a.txt","D:\\a.txt","D:\\a.txt","D:\\a.txt","D:\\a.txt" };
	double cost;
	double start = GetTickCount();
	ReadFileS(path, 13);
	double end = GetTickCount();
	cost = difftime(end, start);
	cout << "time diff 1------------------->" << cost << endl;


	start = GetTickCount();
	ReadFileM(path, 13);
	end = GetTickCount();
	cost = difftime(end, start);


	cout << "time diff 2------------------->" << cost << endl;


	system("pause");
}