#include
#include
using namespace std;
struct Count
{
long count1;
char space[64]; // 此处增加64字节的“无用”空间,目的就是使得count1和count2位于不同的Cache行,从而避免Cache行同步引起的“乒乓效应”,可以尝试去掉这64字节的“无用”空间,将会得到令人诧异的运行结果,要探明深层原因,可以搜索一下“乒乓效应 CPU cache”
long count2;
};
DWORD WINAPI Calculate1(void *p)
{
double x = 0,y = 0;
long* c1 = (long*)p;
for(; x <= 0.5;x += 0.0001)
for(y = 0;y <= 1;y += 0.0001)
{
if (x*x + y*y <= 1) (*c1)++;
}
return 0;
}
DWORD WINAPI Calculate2(void *p)
{
double i = 0,j = 0;//16
long* c2 = (long*)p;
for(i=0.5001;i <=1;i+=0.0001)
for(j=0;j <=1;j+=0.0001)
{
if(i*i+j*j <=1) (*c2)++;
}
return 0;
}
int main()
{
DWORD pThreadID;
Count cnt;
DWORD startTime = 0,endTime = 0;
startTime= GetTickCount();
Calculate1(&cnt.count1);
Calculate2(&cnt.count2);
cout << "Serial calculate cost " << GetTickCount() - startTime << "ms." << endl;
cnt.count1 = 0;
cnt.count2 = 0;
startTime= GetTickCount();
HANDLE hThread1 = CreateThread (NULL,
0,
Calculate1,
&cnt.count1,
0,
&pThreadID);
HANDLE hThread2 = CreateThread (NULL,
0,
Calculate2,
&cnt.count2,
0,
&pThreadID);
WaitForSingleObject(hThread1, INFINITE);
WaitForSingleObject(hThread2, INFINITE);
endTime=GetTickCount();
cout << "Parallel calculate cost " << endTime-startTime << "ms." << endl;
}
在本人的双核机子上,此程序运算结果符合双线程比单线程性能提升一倍的预期,但如果将“char space[64]”去掉,两者性能相差无几。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7364811/viewspace-663783/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/7364811/viewspace-663783/