Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <tchar.h>
- #include <iostream>
- #include <Windows.h>
- #include <boost/thread.hpp>
- class CTimer
- {
- static double TicksPerSecond()
- {
- LARGE_INTEGER nFreq;
- ::QueryPerformanceFrequency( &nFreq );
- return nFreq.QuadPart / 1000000.0;
- }
- public:
- CTimer()
- {
- reset();
- }
- //! Returns current (system) time in microseconds.
- double tick() const
- {
- LARGE_INTEGER nCounter;
- ::QueryPerformanceCounter( &nCounter );
- return nCounter.QuadPart / m_sdTicksPerSecond;
- }
- //! Resets timer and returns current (system) time in microseconds.
- double reset()
- {
- m_dLast = tick();
- return m_dLast;
- }
- //! Returns elapsed time in microseconds since last reset(), then resets timer.
- double lap()
- {
- double t = m_dLast;
- m_dLast = tick();
- return m_dLast - t;
- }
- private:
- double m_dLast;
- static double m_sdTicksPerSecond;
- };
- double CTimer::m_sdTicksPerSecond = CTimer::TicksPerSecond();
- #define N 100000000
- #define K 10
- volatile long n = 0;
- void TP_NL()
- {
- __asm mov eax, 1;
- for( size_t i = 0; i < N; ++i )
- {
- __asm xadd dword ptr[n], eax;
- }
- }
- void TP_L()
- {
- __asm mov eax, 1;
- for( size_t i = 0; i < N; ++i )
- {
- __asm lock xadd dword ptr[n], eax;
- }
- }
- int _tmain( int argc, _TCHAR* argv[] )
- {
- //SetProcessAffinityMask( GetCurrentProcess(), 1 );
- SetThreadAffinityMask( GetCurrentThread(), 1 );
- auto DoTest =
- []( void (*pFunc)() )
- {
- std::vector< boost::thread > oVec;
- oVec.reserve( K );
- CTimer oTimer;
- for( size_t i = 0; i < K; ++i )
- {
- oVec.push_back( boost::thread( pFunc ) );
- }
- for( size_t i = 0; i < K; ++i )
- {
- oVec[i].join();
- }
- double d = oTimer.lap() * 1e3 / double( N ) / double( K );
- std::cout << "const of one operation: " << d << " ns." << std::endl;
- };
- DoTest( &TP_NL );
- DoTest( &TP_L );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement