File indexing completed on 2024-04-28 05:46:51

0001 /*****************************************************************************
0002  *   Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
0003  *                                                                           *
0004  *   This program is free software; you can redistribute it and/or modify    *
0005  *   it under the terms of the GNU Lesser General Public License as          *
0006  *   published by the Free Software Foundation; either version 2.1 of the    *
0007  *   License, or (at your option) version 3, or any later version accepted   *
0008  *   by the membership of KDE e.V. (or its successor approved by the         *
0009  *   membership of KDE e.V.), which shall act as a proxy defined in          *
0010  *   Section 6 of version 3 of the license.                                  *
0011  *                                                                           *
0012  *   This program is distributed in the hope that it will be useful,         *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
0015  *   Lesser General Public License for more details.                         *
0016  *                                                                           *
0017  *   You should have received a copy of the GNU Lesser General Public        *
0018  *   License along with this library. If not,                                *
0019  *   see <http://www.gnu.org/licenses/>.                                     *
0020  *****************************************************************************/
0021 
0022 #include "timer.h"
0023 #include "thread.h"
0024 
0025 #include <chrono>
0026 #include <vector>
0027 
0028 namespace QtCurve {
0029 
0030 QTC_EXPORT uint64_t
0031 getTime()
0032 {
0033     using namespace std::chrono;
0034     return time_point_cast<nanoseconds>(high_resolution_clock::now())
0035         .time_since_epoch().count();
0036 }
0037 
0038 QTC_EXPORT uint64_t
0039 getElapse(uint64_t prev)
0040 {
0041     return getTime() - prev;
0042 }
0043 
0044 static ThreadLocal<std::vector<uint64_t> > tics_list;
0045 
0046 QTC_EXPORT void
0047 tic()
0048 {
0049     tics_list->push_back(0);
0050     auto &back = tics_list->back();
0051     back = getTime();
0052 }
0053 
0054 QTC_EXPORT uint64_t
0055 toc()
0056 {
0057     uint64_t cur_time = getTime();
0058     if (!tics_list->size()) {
0059         return 0;
0060     }
0061     uint64_t old_time = tics_list->back();
0062     tics_list->pop_back();
0063     return cur_time - old_time;
0064 }
0065 
0066 }