计时的性能分析
来源:互联网 发布:阿里云免费邮箱 - 百度 编辑:程序博客网 时间:2024/04/29 07:27
/******************************************************************************Module: UserSyncCompare.cppNotices: Copyright (c) 2008 Jeffrey Richter & Christophe Nasarre******************************************************************************/#include "..\CommonFiles\CmnHdr.h" /* See Appendix A. */#include <windows.h>#include <stdio.h>#include <tchar.h>// Stop watch class from Chapter 7class CStopwatch {public: CStopwatch() { QueryPerformanceFrequency(&m_liPerfFreq); Start(); } void Start() { QueryPerformanceCounter(&m_liPerfStart); } // Returns # of milliseconds since Start was called __int64 Now() const { LARGE_INTEGER liPerfNow; QueryPerformanceCounter(&liPerfNow); return(((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000) / m_liPerfFreq.QuadPart); }private: LARGE_INTEGER m_liPerfFreq; // Counts per second LARGE_INTEGER m_liPerfStart; // Starting count};DWORD g_nIterations = 1000000;typedef void (CALLBACK* OPERATIONFUNC)();DWORD WINAPI ThreadIterationFunction(PVOID operationFunc) {OPERATIONFUNC op = (OPERATIONFUNC) operationFunc; for (DWORD iteration = 0; iteration < g_nIterations; iteration++) { op(); } return 0;}void MeasureConcurrentOperation( TCHAR* operationName, DWORD nThreads, OPERATIONFUNC operationFunc) { HANDLE* phThreads = new HANDLE[nThreads]; SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST); for (DWORD currentThread = 0; currentThread < nThreads; currentThread++) { phThreads[currentThread] = CreateThread(NULL, 0, ThreadIterationFunction, operationFunc, 0, NULL); } SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL); CStopwatch watch; WaitForMultipleObjects(nThreads, phThreads, TRUE, INFINITE); __int64 elapsedTime = watch.Now(); _tprintf( TEXT("Threads=%u, Milliseconds=%u, Test=%s\n"), nThreads, (DWORD)elapsedTime, operationName); // Don't forget to clean up the thread handles for (DWORD currentThread = 0; currentThread < nThreads; currentThread++) { CloseHandle(phThreads[currentThread]); } delete phThreads;}// -----------------------------------------------------------// List of tests to run:// -----------------------------------------------------------// Reading from a volatile int with NO synchronization at all// Writing to an int using InterlockedIncrement// Reading from a volatile int using critical sections// Read from a volatile int using SRWLock// Read from a volatile int using Mutex// -----------------------------------------------------------volatile LONG gv_value = 0;// 'lValue': local variable is initialized but not referenced#pragma warning(disable:4189)void WINAPI VolatileReadCallback(){ LONG lValue = gv_value; }#pragma warning(default:4189)void WINAPI VolatileWriteCallback(){ gv_value = 0; }void WINAPI InterlockedIncrementCallback(){InterlockedIncrement(&gv_value);}CRITICAL_SECTION g_cs;void WINAPI CriticalSectionCallback(){EnterCriticalSection(&g_cs);gv_value = 0;LeaveCriticalSection(&g_cs);}HANDLE g_hMutex;void WINAPI MutexCallback(){WaitForSingleObject(g_hMutex, INFINITE);gv_value = 0;ReleaseMutex(g_hMutex);}// Slim Reader/Writer Lock global variableSRWLOCK g_srwLock;void WINAPI SRWLockReadCallback() {AcquireSRWLockShared(&g_srwLock);gv_value = 0;ReleaseSRWLockShared(&g_srwLock);}void WINAPI SRWLockWriteCallback() {AcquireSRWLockExclusive(&g_srwLock);gv_value = 0;ReleaseSRWLockExclusive(&g_srwLock);}int _tmain(int argc, _TCHAR* argv[]) { for (int nThreads = 1; nThreads <= 4; nThreads *= 2) { MeasureConcurrentOperation(TEXT("Volatile Read"), nThreads, VolatileReadCallback);MeasureConcurrentOperation(TEXT("Volatile Write"), nThreads, VolatileWriteCallback);MeasureConcurrentOperation(TEXT("Interlocked Increment"), nThreads, InterlockedIncrementCallback);// Prepare the critical sectionInitializeCriticalSection(&g_cs);MeasureConcurrentOperation(TEXT("Critical Section"), nThreads, CriticalSectionCallback);// Don't forget to cleanupDeleteCriticalSection(&g_cs); // Prepare the Slim Reader/Writer lockInitializeSRWLock(&g_srwLock);MeasureConcurrentOperation(TEXT("SRWLock Read"), nThreads, SRWLockReadCallback);MeasureConcurrentOperation(TEXT("SRWLock Write"), nThreads, SRWLockWriteCallback);// NOTE: You can't cleanup a Slim Reader/Writer lock// Prepare the mutexg_hMutex = CreateMutex(NULL, false, NULL);MeasureConcurrentOperation(TEXT("Mutex"), nThreads, MutexCallback);CloseHandle(g_hMutex);_tprintf(TEXT("\n"));} system("pause");return(0);}
Threads=1, Milliseconds=33, Test=Volatile Read
Threads=1, Milliseconds=30, Test=Volatile Write
Threads=1, Milliseconds=47, Test=Interlocked Increment
Threads=1, Milliseconds=71, Test=Critical Section
Threads=1, Milliseconds=80, Test=SRWLock Read
Threads=1, Milliseconds=63, Test=SRWLock Write
Threads=1, Milliseconds=953, Test=Mutex
Threads=2, Milliseconds=34, Test=Volatile Read
Threads=2, Milliseconds=42, Test=Volatile Write
Threads=2, Milliseconds=67, Test=Interlocked Increment
Threads=2, Milliseconds=211, Test=Critical Section
Threads=2, Milliseconds=0, Test=SRWLock Read
Threads=2, Milliseconds=88, Test=SRWLock Write
Threads=2, Milliseconds=3668, Test=Mutex
Threads=4, Milliseconds=36, Test=Volatile Read
Threads=4, Milliseconds=83, Test=Volatile Write
Threads=4, Milliseconds=70, Test=Interlocked Increment
Threads=4, Milliseconds=374, Test=Critical Section
Threads=4, Milliseconds=184, Test=SRWLock Read
Threads=4, Milliseconds=198, Test=SRWLock Write
Threads=4, Milliseconds=8474, Test=Mutex
请按任意键继续. . .
- 计时的性能分析
- 软件性能计时
- 软件性能计时
- 高性能服务器设计时需要考虑的几个问题
- 顺序查找与折半查找的性能比较及C++ 计时函数的介绍
- ACE的性能分析
- 反射的性能分析
- vs2012的性能分析
- 性能分析的目的
- 性能分析的目的
- 系统的性能分析
- 算法的性能分析
- 给程序计时2-bzero & memset置零的性能比较
- 系统设计时关于性能问题处理的几点心得
- 系统设计时关于性能问题处理的几点心得
- 纳秒级的计时精度
- 各种操作的计时
- 纳秒级的计时精度
- 已知的CPropertysheet bug: 切换焦点导致无响应
- android 获取本机号码及应加的权限 代码
- poj 1270
- poj-2601Simple calculations
- java通过线程控制程序执行超时(新)_hanCSDN_20130816
- 计时的性能分析
- FormClosing事件
- 修改Fedora 14屏幕分辨率
- 【电脑教程§系统中的任务管理器存在哪些你不知道的功能§】
- How to write a product backlog step by step
- 管理行迁移
- 2013互联网大会,中搜宣布进入移动电商领域
- 相对定位和绝对定位
- (android地图开发) 高德地图手势切换