我有以下代码用于矩阵向量乘法:
std::chrono::steady_clock::time_point start, end2;
void fillMatrixConditions(LPVOID lpv) {
end2 = std::chrono::steady_clock::now();
std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::microseconds>(end2 - start).count() << std::endl;
std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end2 - start).count() << std::endl;
int i, j = horizontControl;
for (i = 0; i < horizontControl; i++, j++) {
b[i] = akcni - uMin;
b[j] = uMax - akcni;
}
j = 2 * horizontControl + N - N1 + 1;
int k = 0;
for (i = 2 * i; i < (2 * horizontControl + N - N1 + 1); i++, j++, k++) {
b[i] = MpDup[k] - yMin;
b[j] = yMax - MpDup[k];
};
RtPrintf("Thread");
}
int _tmain(int argc, _TCHAR * argv[])
{
HANDLE hThread;
DWORD id;
int k = 0;
double temp;
double g[50];
for (int j = 0; j < N - N1 + 1; j++)
{
temp = 0;
for (int m = 0; m < horizontPrediction - 1; m++, k++)
{
temp = temp + Mp[k] * dup[m];
}
MpDup[j] = temp;
tempMatrix[j] = setPoint - y - temp;
}
start = std::chrono::steady_clock::now();
hThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)fillMatrixConditions, NULL, NULL, &id);
k = 0;
for (int j = 0; j < horizontControl; j++)
{
temp = 0;
for (int m = 0; m < N - N1 + 1; m++, k++)
{
temp = temp + Mtranspose[k] * tempMatrix[m];
}
g[j] = -2 * temp;
}
RtPrintf("Point\n");
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
return 0;
}
但是CreateThread()
太慢了,因为这是程序的输出:
Point
Time difference = 247
Time difference = 247261
我认为要写的第一件事就是线程中的Time difference
,然后是&#34; Point&#34;。或者这个输出正常吗?我必须使用CreateThread()
。我不能pthread
等Matrix Mtranspose
有2025
个单元格。
答案 0 :(得分:4)
这只是一种非常复杂的问题:
CreateThread开始执行我的代码需要247微秒是否正常?
答案是肯定的,可能是正常的。这里有一个关于这个主题的问题和答案,它说创建一个线程需要0.015625毫秒,这是15微秒:How long does thread creation and termination take under Windows?比你的时间更快,但不是瞬时。
如果您需要线程更快地启动,您应该使用线程池并提前启动线程。