1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 
3 /*
4  *  Main authors:
5  *     Guido Tack <guido.tack@monash.edu>
6  */
7 
8 /* This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
11 
12 #pragma once
13 
14 #include <chrono>
15 #include <ctime>
16 #include <iomanip>
17 #include <ratio>
18 #include <sstream>
19 
20 namespace MiniZinc {
21 
22 class Timer {
23 protected:
24   std::chrono::steady_clock::time_point _last;
25 
26 public:
27   /// Construct timer
Timer()28   Timer() : _last(std::chrono::steady_clock::now()) {}
29   /// Reset timer
reset()30   void reset() { _last = std::chrono::steady_clock::now(); }
31   /// Return milliseconds since timer was last reset
ms() const32   long long int ms() const {
33     return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() -
34                                                                  _last)
35         .count();
36   }
37   /// Return seconds since timer was last reset
s() const38   double s() const {
39     return std::chrono::duration_cast<std::chrono::duration<double> >(
40                std::chrono::steady_clock::now() - _last)
41         .count();
42   }
stoptime() const43   std::string stoptime() const {
44     std::ostringstream oss;
45     oss << std::setprecision(2) << std::fixed << s() << " s";
46     return oss.str();
47   }
48 };
49 
50 }  // namespace MiniZinc
51