Flow 1.0.0
Flow project: Full implementation reference.
timed_function.hpp
Go to the documentation of this file.
1/* Flow
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
19#pragma once
20
24
25namespace flow::perf
26{
27// Free functions: in *_fwd.hpp.
28
29// Template implementations.
30
31template<typename Accumulator, typename Func>
32auto timed_function(Clock_type clock_type, Accumulator* accumulator, Func&& function)
33{
34 return [clock_type, accumulator, function = std::move(function)](auto&&... params) mutable
35 {
36 const auto start = Checkpointing_timer::now(clock_type);
37
38 function(std::forward<decltype(params)>(params)...);
39
40 /* If Accumulator is atomic<duration_rep_t>: this is fetch_add() with memory ordering arg = strict.
41 * @todo A non-strict-ordering-using API may be desirable. */
42 *accumulator += (Checkpointing_timer::now(clock_type) - start).count();
43 };
44}
45
46template<typename Accumulator, typename Func>
47auto timed_function_nvr(Clock_type clock_type, Accumulator* accumulator, Func&& function)
48{
49 // It's much like timed_function() -- just adding passing-through function() return value. Comments kept light.
50
51 return [clock_type, accumulator, function = std::move(function)](auto&&... params) mutable
52 {
53 const auto start = Checkpointing_timer::now(clock_type);
54
55 // Copy elision should avoid copying the result here.
56 const auto result = function(std::forward<decltype(params)>(params)...);
57
58 *accumulator += (Checkpointing_timer::now(clock_type) - start).count();
59
60 /* I (ygoldfel) *think* this will be copy/move-elided... but not sure.
61 * If yes, then this is good. If not, then should add std::move(), to make sure it doesn't get copied (ugh).
62 * @todo Look into it, maybe experimentally! */
63 return result;
64 };
65}
66
67} // namespace flow::perf
Time_pt_set now() const
Sample all currently enabled Clock_types' clocks and return those values, each of which is a time sta...
Flow module containing tools for profiling and optimization.
auto timed_function(Clock_type clock_type, Accumulator *accumulator, Func &&function)
Constructs a closure that times and executes void-returning function(), adding the elapsed time with ...
auto timed_function_nvr(Clock_type clock_type, Accumulator *accumulator, Func &&function)
Constructs a closure that times and executes non-void-returning function(), adding the elapsed time w...
Clock_type
Clock types supported by flow::perf module facilities, perf::Checkpointing_timer in particular.