close

 http://hi.baidu.com/wadeswb/blog/item/12bd4c19868c030c34fa4129.html
 需include xmmintrin.h 和 windows.h
 
// Timer Class
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
arrow
arrow
    全站熱搜

    chunyuan 發表在 痞客邦 留言(0) 人氣()