【发布时间】:2010-01-12 18:56:27
【问题描述】:
第一个数据集执行时间增加的原因是什么?组装说明是相同的。
DN_FLUSH 标志未打开时,第一个数据集需要 63 毫秒,第二个数据集需要 15 毫秒。
开启 DN_FLUSH 标志后,第一个数据集需要 15 毫秒,第二个数据集需要 ~0 毫秒。
因此,在这两种情况下,第一个数据集的执行时间要长得多。
有没有什么办法可以减少执行时间以更接近第二个数据集?
我正在使用 C++ Visual Studio 2005,/arch:SSE2 /fp:fast 在 Intel Core 2 Duo T7700 @ 2.4Ghz Windows XP Pro 上运行。
#define NUMLOOPS 1000000
// Denormal values flushed to zero by hardware on ALPHA and x86
// processors with SSE2 support. Ignored on other x86 platforms
// Setting this decreases execution time from 63 milliseconds to 16 millisecond
// _controlfp(_DN_FLUSH, _MCW_DN);
float denormal = 1.0e-38;
float denormalTwo = 1.0e-39;
float denormalThree = 1;
tickStart = GetTickCount();
// Run First Calculation Loop
for (loops=0; loops < NUMLOOPS; loops++)
{
denormalThree = denormal - denormalTwo;
}
// Get execution time
duration = GetTickCount()-tickStart;
printf("Duration = %dms\n", duration);
float normal = 1.0e-10;
float normalTwo = 1.0e-2;
float normalThree = 1;
tickStart = GetTickCount();
// Run Second Calculation Loop
for (loops=0; loops < NUMLOOPS; loops++)
{
normalThree = normal - normalTwo;
}
// Get execution time
duration = GetTickCount()-tickStart;
printf("Duration = %dms\n", duration);
【问题讨论】:
-
您应该知道 GetTickCount() 对于计时代码几乎毫无价值。它的粒度往往非常大,可能高达 100 毫秒,因系统而异。请改用 QueryPerformanceCounter()。
-
QueryPerformanceCounter() 使用起来很痛苦……试试 timeGetTime()。此外,GetTickCount() 对于运行几秒钟或更长时间的事情并没有那么糟糕 - 请注意准确性。
-
顺便说一句,不要假设使用 /arch:SSE2 /fp:fast 实际上会使您的代码更快。对于我的代码,我发现 /fp:precise 和使用 FP 堆栈实际上更快。同样,不要假设浮点数比双精度数更快。测试所有选项。
标签: c++ floating-point x86