SIRIUS 7.5.0
Electronic structure library and applications
time_tools.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2023 Anton Kozhevnikov, Thomas Schulthess
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
5// the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8// following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
10// and the following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
13// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
15// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
18// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20/** \file time_tools.hpp
21 *
22 * \brief Timing functions.
23 */
24
25#ifndef __TIME_TOOLS_HPP__
26#define __TIME_TOOLS_HPP__
27
28#include <sys/time.h>
29
30namespace sirius {
31
32/// Return the timestamp string in a specified format.
33/** Typical format strings: "%Y%m%d_%H%M%S", "%Y-%m-%d %H:%M:%S", "%H:%M:%S" */
34inline auto timestamp(std::string fmt)
35{
36 timeval t;
37 gettimeofday(&t, NULL);
38
39 char buf[128];
40
41 tm* ptm = localtime(&t.tv_sec);
42 strftime(buf, sizeof(buf), fmt.c_str(), ptm);
43 return std::string(buf);
44}
45
46/// Wall-clock time in seconds.
47inline double wtime()
48{
49 timeval t;
50 gettimeofday(&t, NULL);
51 return double(t.tv_sec) + double(t.tv_usec) / 1e6;
52}
53
54using time_point_t = std::chrono::high_resolution_clock::time_point;
55
56inline auto time_now()
57{
58 return std::chrono::high_resolution_clock::now();
59}
60
61inline double time_interval(std::chrono::high_resolution_clock::time_point t0)
62{
63 return std::chrono::duration_cast<std::chrono::duration<double>>(time_now() - t0).count();
64}
65
66}
67
68#endif
69
Namespace of the SIRIUS library.
Definition: sirius.f90:5
auto timestamp(std::string fmt)
Return the timestamp string in a specified format.
Definition: time_tools.hpp:34
double wtime()
Wall-clock time in seconds.
Definition: time_tools.hpp:47