⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simple_timer.h

📁 这是一个简单的时间计算类,我在写程序时经常用到,所以也想到大家也可能用到,
💻 H
字号:
/**********************************************************************************
*                                                                                 *
*  Copyright (c) 2003 - 2004 by Royal. All rights reserved.                       *
*                                                                                 *
*  Permission to use, copy, modify, and distribute this software for any purpose  *
*  is hereby granted without fee, provided that this copyright and permissions    *
*  notice appear in all copies and derivatives, and that no charge may be made    *
*  for the software and its documentation except to cover cost of distribution.   *
*                                                                                 *
*  This software is provided "as is" without express or implied warranty.         *
*                                                                                 *
**********************************************************************************/

/*
*  Description:
*
*    SimpleTimer is used to keep track of CPU time used.
*
*  History:
*
*    Initial version created by Royal, May, 2004.
*    Updated by Royal, July, 2004.
*
*  Notes:
*
*    This code has been written to conform to standard C++ and STL. It has been
*    compiled successfully using GNU C++ 3.2, Borland C++ 5.5, and Visual C++ 7.0.
*/

#ifndef SIMPLE_TIMER_H
#define SIMPLE_TIMER_H

#include <ctime>
#include <cstdlib>
#include <string>

class SimpleTimer
{
public:
    SimpleTimer()
    {
        reset();
    }

    operator double() const
    {
        return (std::clock() - start_) / static_cast<double>(CLOCKS_PER_SEC);
    }
     
    void reset()
    {
        start_ = std::clock();
    }

    std::string now()
    {
        std::time_t t = time(0);
        std::tm* now = std::localtime(&t);
        int h, m, s;
        h = now->tm_hour;
        m = now->tm_min;
        s = now->tm_sec;
        char c[12];
        std::sprintf(c, "%d:%d:%d", h, m, s);
        return std::string(c);
    }

private:
    std::clock_t start_;
};

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -