头文件:
/* * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), [email protected] * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 2 or any later version. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. A copy of the GNU General Public License is available at: * http://www.fsf.org/licensing/licenses */ /***************************************************************************** * timing.h * * Making a coarse estimation for programm's running time. The time unit * is second. * * Zhang Ming, 2010-01, Xi'an Jiaotong University. *****************************************************************************/ #ifndef TIMING_H #define TIMING_H #include <ctime> inline static double seconds() { const double secs_per_tick = 1.0 / CLOCKS_PER_SEC; return ( double(clock()) ) * secs_per_tick; } namespace splab { class Timing { public: Timing(); ~Timing(); void start(); void stop(); double read(); private: bool running; double startTime; double total; }; // class Timing #include <timing-impl.h> } // namespace splab #endif // TIMING_H
实现文件:
/* * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), [email protected] * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 2 or any later version. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. A copy of the GNU General Public License is available at: * http://www.fsf.org/licensing/licenses */ /***************************************************************************** * timing-impl.h * * Implementation for Timing class. * * Zhang Ming, 2010-01, Xi'an Jiaotong University. *****************************************************************************/ /** * constructor and destructor */ Timing::Timing() : running(false), startTime(0), total(0) { } Timing::~Timing() { } /** * timing start */ inline void Timing::start() { running = true; total = 0; startTime = seconds(); } /** * timing stop */ inline void Timing::stop() { if( running ) { total += ( seconds() - startTime ); running = false; } } /** * Get the time between start() and stop(). */ inline double Timing::read() { if( running ) stop(); return total; }
测试代码:
/***************************************************************************** * time_test.cpp * * Timing function testing. * * Zhang Ming, 2010-01, Xi'an Jiaotong University. *****************************************************************************/ #include <iostream> #include <cmath> #include <timing.h> using namespace std; using namespace splab; int main() { double var = 1; double runTime = 0; Timing time; time.start(); for( int i=0; i<10000; ++i ) for( int j=0; j<10000; ++j ) var = sqrt(1.0*i*j); time.stop(); runTime = time.read(); cout << "The running time is : " << runTime << endl << endl; return 0; }
运行结果:
The running time is : 7.239 Process returned 0 (0x0) execution time : 7.332 s Press any key to continue.