Visual C++SSE函数在没有调试器的情况下启动时速度较慢
Visual C++ SSE function slow when starting without debugger
关于SSE的使用,我遇到了一个非常奇怪的问题。
我编写了以下函数,其中我使用SSE来计算两个浮点数组的差值的最大值,每个数组包含64个浮点。
dists数组是通过_aligned_malloc
分配的2d数组。
#include <iostream>
#include <xmmintrin.h>
#include <time.h>
#include <stdio.h>
#include <algorithm>
#include <fstream>
#include "hr_time.h"
using namespace std;
float** dists;
float** dists2;
__m128* a;
__m128* b;
__m128* c;
__m128* d;
__m128 diff;
__m128 diff2;
__m128 mymax;
float* myfmax;
float test(int s, int t)
{
a = (__m128*) dists[s];
b = (__m128*) dists[t];
c = (__m128*) dists2[s];
d = (__m128*) dists2[t];
diff;
mymax = _mm_set_ps(0.0, 0.0, 0.0, 0.0);
for (int i = 0; i <= 16; i++)
{
diff = _mm_sub_ps(*a, *b);
mymax = _mm_max_ps(diff, mymax);
diff2 = _mm_sub_ps(*d, *c);
mymax = _mm_max_ps(diff2, mymax);
a++;
b++;
c++;
d++;
}
_mm_store_ps(myfmax, mymax);
float res = max(max(max(myfmax[0], myfmax[1]), myfmax[2]), myfmax[3]);
return res;
}
int Deserialize(std::istream* stream)
{
int numOfElements, arraySize;
stream->read((char*)&numOfElements, sizeof(int)); // numOfElements = 64
stream->read((char*)&arraySize, sizeof(int)); // arraySize = 8000000
dists = (float**)_aligned_malloc(arraySize * sizeof(float*), 16);
dists2 = (float**)_aligned_malloc(arraySize * sizeof(float*), 16);
for (int j = 0; j < arraySize; j++)
{
dists[j] = (float*)_aligned_malloc(numOfElements * sizeof(float), 16);
dists2[j] = (float*)_aligned_malloc(numOfElements * sizeof(float), 16);
}
for (int i = 0; i < arraySize; i++)
{
stream->read((char*)dists[i], (numOfElements*sizeof(float)));
}
for (int i = 0; i < arraySize; i++)
{
stream->read((char*)dists2[i], (numOfElements*sizeof(float)));
}
return 0;
}
int main(int argc, char** argv)
{
int entries = 8000000;
myfmax = (float*)_aligned_malloc(4 * sizeof(float), 16);
ifstream fs("binary_file", std::ios::binary);
Deserialize(&fs);
CStopWatch* watch = new CStopWatch();
watch->StartTimer();
int i;
for (i = 0; i < entries; i++)
{
int s = rand() % entries;
int t = rand() % entries;
test(s, t);
}
watch->StopTimer();
cout << i << " iterations took " << watch->GetElapsedTimeMs() << "ms" << endl;
cin.get();
}
我的问题是,如果我在带有调试器的Visual Studio中运行此代码,那么它的运行速度会非常快。但是,一旦我在没有调试器的情况下执行它,它就会变得非常慢。所以我做了一点研究,发现这两种启动方法之间的一个区别是"调试堆"。所以我通过定义"_NO_DEBUG_HAP=1"来禁用它。有了这个选项,附加的调试器的性能也很差。
但我不明白如何使用调试堆来获得更好的性能?我不知道如何解决这个问题,所以我希望你们中的一个人能帮助我
提前谢谢。
谨致问候,Karsten
您的代码有一个错误。_mm_store_ps
存储一个由四个浮点组成的数组,但您只声明一个。编译器甚至不应该允许你这样做。
更改
float fmax;
_mm_store_ps(fmax, max);
pi = std::max(std::max(std::max(fmax[0], fmax[1]), fmax[2]), fmax[3]);
至
float __declspec(align(16)) fmax[4];
_mm_store_ps(fmax, max);
return std::max(std::max(std::max(fmax[0], fmax[1]), fmax[2]), fmax[3]);
相关文章:
- 在没有管理员权限的情况下,在 c++ 中以编程方式将程序添加到启动
- C++ POCO - 如何在不使用 run() 方法的情况下启动线程池上的线程?
- 有没有办法在不重新启动或注销的情况下加载游标?
- (Windows 10) 在不更新的情况下重新启动C++
- 为什么在 var 未更新的情况下使用,仅在启动 var 后打印
- 在不定义类的情况下将启动的类传递给函数
- RCpp:如何在不重新启动R的情况下重新加载重新编译的C++代码
- 在存在分配器重新启动的情况下,使用null_mutex Safe是boost :: fast_pool_allocato
- Visual Studio:如何在没有控制台窗口的情况下启动项目
- 在不调试的情况下启动,与从.exe启动相同
- Qt/QML:如何在不重新启动应用程序的情况下"switch to"或"open"另一个 QML 文件?
- 如何在不闪烁的情况下以自定义大小启动控制台
- 在没有权限的情况下在登录时启动程序(Fedora 20)
- 如何让火狐在不重新启动的情况下重新加载user_pref
- 是否可以在不使用 SIGHUP 的情况下立即启动 pthread
- c++如何在没有新的情况下启动
- Mac GUI应用程序如何在不使用Sparkle的情况下重新启动自己?
- Visual C++SSE函数在没有调试器的情况下启动时速度较慢
- 是否可以在不显示窗口的情况下启动命令行应用程序
- 如何在没有控制台窗口的情况下从c++启动.jar文件