我正在用C++做一些性能关键的工作,我们目前正在使用整数计算来解决本质上浮点的问题,因为"它更快".这会导致很多令人烦恼的问题,并增加了许多烦人的代码.
现在,我记得在大约386天的时间里读到关于浮点计算如此缓慢的情况,我相信(IIRC)有一个可选的共同进程.但是现在肯定会有指数级更复杂和更强大的CPU,如果进行浮点或整数计算,它在"速度"上没有区别吗?特别是因为与导致管道停滞或从主存储器中取出某些东西相比,实际计算时间很短?
我知道正确的答案是在目标硬件上进行基准测试,测试它的好方法是什么?我编写了两个很小的C++程序,并将它们的运行时间与Linux上的"时间"进行了比较,但实际的运行时间变化太大(无法帮助我在虚拟服务器上运行).没有花一整天的时间来运行数百个基准测试,制作图表等等,我可以做些什么来合理地测试相对速度?任何想法或想法?我完全错了吗?
我使用的程序如下,它们不相同:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <time.h>
int main( int argc, char** argv )
{
int accum = 0;
srand( time( NULL ) );
for( unsigned int i = 0; i < 100000000; ++i )
{
accum += rand( ) % 365;
}
std::cout << accum << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
计划2:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <time.h>
int main( int argc, char** argv )
{
float accum = 0;
srand( time( NULL …Run Code Online (Sandbox Code Playgroud) 是什么原因导致第一个数据集的执行时间增加了?装配说明是相同的.
如果DN_FLUSH标志未打开,则第一个数据集需要63毫秒,第二个数据集需要15毫秒.
在DN_FLUSH标志打开的情况下,第一个数据集需要15毫秒,第二个数据集需要约0毫秒.
因此,在这两种情况下,第一数据集的执行时间都要大得多.
有没有办法减少执行时间与第二个数据集更接近?
我正在使用C++ Visual Studio 2005,/ arch:SSE2/fp:在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 …Run Code Online (Sandbox Code Playgroud)