- 论坛徽章:
- 0
|
回复 4# OwnWaterloo
我用的VS专业版,有单元测试和顺序测试,不知道TeamSuite是不是有更多的测试。
gprof这类的测试的确很强大。虽然没有用它来测试我目前的项目,但是手头项目的函数很多,相比输出肯定很杂乱,然后又是C++的项目,里面逻辑并不是函数调用那么简单,比如同一组虚函数的实现最好能合并等等。
而且这种工具会往代码里面加点东西,这也会影响最后的结果。不过效果应该比我手动添加计时点要好。
目前我的做法就是针对疑似性能低下的部分手动安插计时器进行计时。- #ifndef __PERF_HEADER__
- #ifdef LOCAL_PERF
- #undef LOCAL_PERF
- #endif
- #ifdef CUSTOM_PERF
- #undef CUSTOM_PERF
- #endif
- #if defined(COMPILER_NDEBUG) && defined(COMPILER_PERF)
- #define __PERF_HEADER__
- #include "timecounter.h"
- // print performance on destruction
- struct local_perf
- {
- local_perf(const char *func) : _func_name(func) {};
- ~local_perf()
- {
- unsigned long span = tc.time();
- unicout << _T("\"") << _func_name << _T("\" spent ") << span << _T("ms") << std::endl;
- };
- const char *_func_name;
- time_counter tc;
- };
- #define LOCAL_PERF() local_perf __local_perf__(__FUNCTION__)
- #define CUSTOM_PERF(func) local_perf __custom_perf__(func)
- #else
- #define LOCAL_PERF()
- #define CUSTOM_PERF(func)
- #endif
- #ifdef ACC_PERF
- #undef ACC_PERF
- #endif
- #ifdef CUSTOM_ACC_PERF
- #undef CUSTOM_ACC_PERF
- #endif
- #if defined(COMPILER_NDEBUG) && defined(COMPILER_PERF)
- // print performance on destruction, but can accumulate total time for a function
- template <typename tag>
- struct accumulated_perf
- {
- struct static_perf
- {
- unsigned long _total;
- const tchar *_func;
- static_perf(const tchar *func)
- : _total(0), _func(func) {}
- ~static_perf()
- {
- unicout << _T("\"") << _func << _T("\" spent ") << _total << _T("ms") << std::endl;
- }
- };
- public:
- accumulated_perf(static_perf &perf) : _func_perf(perf) {}
- ~accumulated_perf() {_func_perf._total += _tc.time();}
- private:
- time_counter _tc;
- static_perf &_func_perf;
- };
- #define ACC_PERF() CUSTOM_ACC_PERF(__FUNCTION__)
- #define CUSTOM_ACC_PERF(info) struct __anonymous__{};static accumulated_perf<__anonymous__>::static_perf __static_perf__(info); accumulated_perf<__anonymous__> __accumulated_perf__(__static_perf__)
- #else
- #define ACC_PERF()
- #define CUSTOM_ACC_PERF(info)
- #endif
- #endif
复制代码 确定只调用一次的用前面的LOCAL_PERF,确定会调用多次的用后面的ACC_PERF,只测试某函数的部分代码的用CUSTOM_XXX版本。
原理跟那些自动化工具其实差不多。就是发现GetThreadTimes速度太慢,严重影响结果,GetTickCount又会受到其他进程的影响,鱼和熊掌不可兼得啊。 |
|