可悲的是,答案比这些建议要平凡得多,尽管它们确实有帮助。基本上,我搞砸了我的计时方式。
我使用的计时代码是这样的:
Ipp32s timer;
ippGetCpuFreqMhz(&timer);
Ipp64u globalStart = ippGetCpuClocks();
globalStart = ippGetCpuClocks() *2 - globalStart; //use this method to get rid of the overhead of getting clock ticks
//do some stuff
Ipp64u globalEnd = ippGetCpuClocks();
globalEnd = ippGetCpuClocks() *2 - globalEnd;
std::cout << "total runtime: " << ((Ipp64f)globalEnd - (Ipp64f)globalStart)/((Ipp64f)timer *1000000.0f) << " seconds" << std::endl;
此代码特定于英特尔编译器,旨在提供极其精确的时间测量。不幸的是,这种极高的精度意味着每次运行的成本约为 2.5 秒。删除时序代码消除了该时间限制。
不过,运行时间似乎仍然存在延迟——代码将报告 0.24 s 与该计时代码,现在报告大约 0.35 秒的时间,这意味着大约有 50% 的速度成本。
将代码更改为:
static extern void ImageProcessing(
IntPtr inImage, //[MarshalAs(UnmanagedType.LPArray)]ushort[] inImage,
IntPtr outImage, //[MarshalAs(UnmanagedType.LPArray)]ushort[] outImage,
int inYSize, int inXSize);
并称为:
unsafe {
fixed (ushort* inImagePtr = theInputImage.DataArray){
fixed (ushort* outImagePtr = theResult){
ImageProcessing((IntPtr)inImagePtr,//theInputImage.DataArray,
(IntPtr)outImagePtr,//theResult,
ysize,
xsize);
}
}
}
将可执行时间降至 0.3 秒(三次运行的平均值)。对于我的口味来说仍然太慢了,但是 10 倍的速度提升肯定在我老板可以接受的范围内。