我正在尝试多线程一个程序,它从文件中取出单词然后散列这些并写入另一个程序。
如果我在没有多线程的情况下完成它的速度非常快,那么它只能使用15-20%的CPU并且具有类似于300.000line / s的速度
但是当我尝试使用多线程时,它只会减速并开始仅以17000行/秒的速度散列,你能帮助我吗?
由于
#include <iostream> // std::cout, std::streambuf, std::streamsize
#include <fstream> // std::ifstream
#include <string>
#include <thread>
#include "md5.h"
using namespace std;
static const int num_threads = 10;
void call_from_thread(int tid) {
cout << "Launched by thread " << tid << std::endl;
int cl = 0;
int uscita = 0;
int parole = 0;
char* contents;
ifstream istr("test.txt");
if (istr) {
streambuf * pbuf = istr.rdbuf();
streamsize size = pbuf->pubseekoff(0, istr.end);
pbuf->pubseekoff(0, istr.beg); // rewind
contents = new char[size];
pbuf->sgetn(contents, size);
istr.close();
ofstream myfile;
myfile.open("out.txt");
do {
string prova("");
uscita = 0;
do {
if (contents[cl] == '\n') {
uscita = 1;
}
prova += contents[cl];
cl += 1;
} while (uscita != 1);
parole += 1;
//cout << prova << ":" << md5(prova) << endl;
myfile << prova << ":" << md5(prova) << endl;
} while (parole != 9586054);
myfile.close();
}
}
int main()
{
thread t[num_threads];
//Launch a group of threads
for (int i = 0; i < num_threads; ++i) {
t[i] = thread(call_from_thread, i);
}
cout << "Launched from the main\n";
//Join the threads with the main thread
for (int i = 0; i < num_threads; ++i) {
t[i].join();
}
return 0;
}
答案 0 :(得分:0)
您在此处看到的是,所有线程都在读取相同的文件并执行相同的工作。这导致文件必须被读取import { Component, ElementRef, Input, ViewEncapsulation } from '@angular/core';
import { PickList, DomHandler } from 'primeng/primeng';
import { ObjectUtils } from 'primeng/components/utils/objectutils';
@Component({
selector: 'ex-pick-list',
templateUrl: 'pick-list.component.html',
styleUrls: ['pick-list.component.scss'],
encapsulation: ViewEncapsulation.None
})
export class PickListComponent extends PickList {
constructor(el: ElementRef, domHandler: DomHandler, objectUtils: ObjectUtils) {
super(el, domHandler, objectUtils);
}
}
<div class="pick-list-container">
<!-- put the original html from the primeng pick list component "moveRight()" now you can use moveRight from PickList -->
<div class="custom-buttons">
<button pButton type="button" (click)="moveRight()" class="ui-[label]="''"></button>
</div>
</div>
</div>
次,可能导致约num_threads
的速度减慢(因为文件I / O可能在关键路径上)或者甚至更多因为更多的缓存未命中
您可以尝试将整个文件读入内存,在线程之间拆分,然后让每个线程处理该文件内容的子集。