YSTest: timing.hpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

timing.hpp
浏览该文件的文档.
1 /*
2 © 2012-2014 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YB_INC_ytest_timing_hpp_
29 #define YB_INC_ytest_timing_hpp_ 1
30
31 #include "../ydef.h"
32 #include <ctime>
33
38 namespace ytest
39 {
40
42 using ystdex::size_t;
43
44
49 namespace timing
50 {
51
57 template<typename _fNow, typename _func, typename... _tParams>
58 inline auto
59 once(_fNow now, _func&& f, _tParams&&... args) -> decltype(now() - now())
60 {
61 const auto cl(now());
62
63 yforward(f)(yforward(args)...);
64 return now() - cl;
65 }
66
72 template<typename _func, typename... _tParams>
73 inline double
74 once_c(_func&& f, _tParams&&... args)
75 {
76 const std::clock_t cl(std::clock());
77
78 yforward(f)(yforward(args)...);
79 return double(std::clock() - cl) / CLOCKS_PER_SEC;
80 }
81
82
89 template<class _fNow, typename _func, typename... _tParams>
90 inline auto
91 total(size_t n, _fNow now, _func&& f, _tParams&&... args)
92 -> decltype(now() - now())
93 {
94 const auto cl(now());
95
96 for(size_t i(0); i != n; ++i)
97 yforward(f)(yforward(args)...);
98 return now() - cl;
99 }
100
106 template<typename _func, typename... _tParams>
107 inline double
108 total_c(size_t n, _func&& f, _tParams&&... args)
109 {
110 const std::clock_t cl(std::clock());
111
112 for(size_t i(0); i != n; ++i)
113 yforward(f)(yforward(args)...);
114 return double(std::clock() - cl) / CLOCKS_PER_SEC;
115 }
116
117
122 template<typename... _tParams>
123 inline auto
124 average(size_t n, _tParams&&... args)
125 -> decltype(timing::total(n, yforward(args)...) / n)
126 {
127 return timing::total(n, yforward(args)...) / n;
128 }
129
134 template<typename _tDuration, typename _func>
135 _tDuration
136 average_elapsed(_func f, size_t n = 1, _tDuration elapsed = {})
137 {
138 f(n, elapsed);
139 return elapsed / n;
140 }
141
146 template<typename _tDuration, typename _func>
147 _tDuration
148 average_in_duration(_func f, _tDuration d)
149 {
150 return average_elapsed<_tDuration>([=](size_t& cnt, _tDuration& elapsed){
151 for(; elapsed < d; ++cnt)
152 elapsed += f();
153 });
154 }
155
156 } // namespace timing;
157
158 } // namespace ytest;
159
160 #endif
161
ytest::timing::average
auto average(size_t n, _tParams &&...args) -> decltype(timing::total(n, std::forward< decltype(args)>(args)...)/n)
测试指定函数执行若干次的平均时间。
Definition: timing.hpp:124
ytest::timing::total
auto total(size_t n, _fNow now, _func &&f, _tParams &&...args) -> decltype(now()-now())
测试指定函数执行若干次的总时间。
Definition: timing.hpp:91
ytest::timing::average_in_duration
_tDuration average_in_duration(_func f, _tDuration d)
测试不超过期限的累计平均时间。
Definition: timing.hpp:148
platform::size_t
YF_API size_t
Definition: FileIO.h:1353
CHRLib::f
f(d, s)
ytest
YFramework 基础测试库命名空间。
Definition: test.h:44
CHRLib::args
_tParams && args
Definition: CharacterProcessing.h:538
YSLib::i
return i
Definition: YCoreUtilities.h:176
ytest::timing::once
auto once(_fNow now, _func &&f, _tParams &&...args) -> decltype(now()-now())
测试指定函数执行一次的时间。
Definition: timing.hpp:59
ytest::timing::total_c
double total_c(size_t n, _func &&f, _tParams &&...args)
测试指定函数执行若干次的总时间。
Definition: timing.hpp:108
ytest::timing::average_elapsed
_tDuration average_elapsed(_func f, size_t n=1, _tDuration elapsed={})
测试累计平均时间。
Definition: timing.hpp:136
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:995
CHRLib::d
_tDst * d
Definition: CharacterProcessing.h:139
ytest::timing::once_c
double once_c(_func &&f, _tParams &&...args)
测试指定函数执行一次的时间。
Definition: timing.hpp:74