SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
ExecutionTime.h
1 /*
2 The MIT License (MIT)
3 
4 Copyright (c) 2015 Hideaki Suzuki
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #pragma once
26 
27 #include <chrono>
28 #include <iomanip>
29 #include <string>
30 
31 #ifdef NO_TIME_ELAPSED_MEASUREMENT
32 # define TIME_ELAPSED_N(n,...) time_elapsed_impl_noop<n>([&]{__VA_ARGS__;})
33 # define TIME_ELAPSED_MARKER_N(n,marker,...) time_elapsed_impl_noop<n>([&]{__VA_ARGS__;})
34 #else
35 # define TIME_ELAPSED_N(n,...) time_elapsed_impl1<n>([&]{__VA_ARGS__;},__FILE__,__LINE__)
36 # define TIME_ELAPSED_MARKER_N(n,marker,...) time_elapsed_impl_core<n>([&]{__VA_ARGS__;},marker)
37 #endif
38 
39 #define TIME_ELAPSED(...) TIME_ELAPSED_N(1,__VA_ARGS__)
40 #define TIME_ELAPSED_MARKER(marker,...) TIME_ELAPSED_MARKER_N(1,marker,__VA_ARGS__)
41 
42 template<const int count, typename BlockBody>
43 inline void time_elapsed_impl_core(BlockBody body, const std::string& header) {
44  auto t0 = std::chrono::high_resolution_clock::now();
45  for (int i = 0; i < count; i++)
46  body();
47 
48  auto t1 = std::chrono::high_resolution_clock::now();
49  const float millis = (float) std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0).count() / 1000.0f;
50  ai_log("ElapsedTime: %s, cnt: %i - %0.3fmsec", header.c_str(), count, millis);
51 }
52 
53 template<const int count, typename BlockBody>
54 inline void time_elapsed_impl_noop(BlockBody body) {
55  for (int i = 0; i < count; i++)
56  body();
57 }
58 
59 template<int count, typename BlockBody>
60 inline void time_elapsed_impl1(BlockBody body, const char * const path, const int lno) {
61  std::string basename { path + std::string(path).find_last_of("\\/") + 1 };
62  time_elapsed_impl_core<count>(body, basename + " at L." + std::to_string(lno));
63 }
#define ai_log(...)
Logging macro to provide your own loggers.
Definition: Types.h:16