class CTimer { public: __forceinline CTimer( void ) { QueryPerformanceFrequency( &m_Frequency ); QueryPerformanceCounter( &m_StartCount ); } __forceinline void Reset( void ) { QueryPerformanceCounter( &m_StartCount ); } __forceinline double End( void ) { static __int64 nCurCount; QueryPerformanceCounter( (PLARGE_INTEGER)&nCurCount ); return double( nCurCount * ( *(__int64*)&m_StartCount ) ) / double( *(__int64*)&m_Frequency ); } private: LARGE_INTEGER m_Frequency; LARGE_INTEGER m_StartCount; }; void ScaleValue1( float *pArray, DWORD dwCount, float fScale ) { |
DWORD dwGroupCount = dwCount / 4; __m128 e_Scale = _mm_set_ps1( fScale ); for ( DWORD i = 0; i < dwGroupCount; i++ ) { *(__m128*)( pArray + i * 4 ) = _mm_mul_ps( *(__m128*)( pArray + i * 4 ), e_Scale ); } } void ScaleValue2( float *pArray, DWORD dwCount, float fScale ) { for ( DWORD i = 0; i < dwCount; i++ ) { pArray[i] *= fScale; } } #define ARRAYCOUNT 10000 int __cdecl main() { float __declspec(align(16)) Array[ARRAYCOUNT]; memset( Array, 0, sizeof(float) * ARRAYCOUNT ); CTimer t; double dTime; t.Reset(); |
for ( int i = 0; i < 100000; i++ ) { ScaleValue1( Array, ARRAYCOUNT, 1000.0f ); } dTime = t.End(); cout << "Use SSE:" << dTime << "秒" << endl; t.Reset(); for ( int i = 0; i < 100000; i++ ) { ScaleValue2( Array, ARRAYCOUNT, 1000.0f ); } dTime = t.End(); cout << "Not Use SSE:" << dTime << "秒" << endl; system( "pause" ); return 0; } Use SSE:0.997817 Not Use SSE:2.84963 |
这里要注意一下,我使用了__declspec(align(16))做为数组定义的修释符,这表示该数组是以16字节为边界对齐的,因为SSE指令只能支持这种格式的内存数据。
我们在这里看到了SSE算法的强大,相信它会成为多媒体程序员手中用来对付无穷尽流媒体数据的一把利剑。我后面还会写一些关于SSE算法更复杂应用的文章,敬请关注,感谢您抽时间阅读!








