Main Page | Modules | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

timer.hh

Go to the documentation of this file.
00001 // Copyright (C) 2004  Laboratoire d'Informatique de Paris 6 (LIP6),
00002 // département Systèmes Répartis Coopératifs (SRC), Université Pierre
00003 // et Marie Curie.
00004 //
00005 // This file is part of Spot, a model checking library.
00006 //
00007 // Spot is free software; you can redistribute it and/or modify it
00008 // under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 2 of the License, or
00010 // (at your option) any later version.
00011 //
00012 // Spot is distributed in the hope that it will be useful, but WITHOUT
00013 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00014 // or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
00015 // License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Spot; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00020 // 02111-1307, USA.
00021 
00022 #ifndef SPOT_MISC_TIMER_HH
00023 # define SPOT_MISC_TIMER_HH
00024 
00025 # include <cassert>
00026 # include <iosfwd>
00027 # include <string>
00028 # include <map>
00029 # include <sys/times.h>
00030 
00031 namespace spot
00032 {
00035 
00037   struct time_info
00038   {
00039     time_info()
00040       : utime(), stime(0)
00041     {
00042     }
00043     clock_t utime;
00044     clock_t stime;
00045   };
00046 
00048   class timer
00049   {
00050   public:
00051 
00053     void
00054     start()
00055     {
00056       struct tms tmp;
00057       times(&tmp);
00058       start_.utime = tmp.tms_utime;
00059       start_.stime = tmp.tms_stime;
00060     }
00061 
00063     void
00064     stop()
00065     {
00066       struct tms tmp;
00067       times(&tmp);
00068       total_.utime += tmp.tms_utime - start_.utime;
00069       total_.stime += tmp.tms_stime - start_.stime;
00070     }
00071 
00076     clock_t
00077     utime() const
00078     {
00079       return total_.utime;
00080     }
00081 
00086     clock_t
00087     stime() const
00088     {
00089       return total_.stime;
00090     }
00091 
00092   protected:
00093     time_info start_;
00094     time_info total_;
00095   };
00096 
00101   class timer_map
00102   {
00103   public:
00104 
00110     void
00111     start(const std::string& name)
00112     {
00113       item_type& it = tm[name];
00114       it.first.start();
00115       ++it.second;
00116     }
00117 
00121     void
00122     stop(const std::string& name)
00123     {
00124       tm[name].first.stop();
00125     }
00126 
00134     void
00135     cancel(const std::string& name)
00136     {
00137       tm_type::iterator i = tm.find(name);
00138       assert(i != tm.end());
00139       assert(0 < i->second.second);
00140       if (0 == --i->second.second)
00141         tm.erase(i);
00142     }
00143 
00145     const spot::timer&
00146     timer(const std::string& name) const
00147     {
00148       tm_type::const_iterator i = tm.find(name);
00149       assert(i != tm.end());
00150       return i->second.first;
00151     }
00152 
00154     spot::timer&
00155     timer(const std::string& name)
00156     {
00157       return tm[name].first;
00158     }
00159 
00165     bool
00166     empty() const
00167     {
00168       return tm.empty();
00169     }
00170 
00172     std::ostream&
00173     print(std::ostream& os) const;
00174 
00175   protected:
00176     typedef std::pair<spot::timer, int> item_type;
00177     typedef std::map<std::string, item_type> tm_type;
00178     tm_type tm;
00179   };
00180 
00182 }
00183 
00184 #endif // SPOT_MISC_ESCAPE_HH

Please comment this page and report errors about it on the RefDocComments page.
Generated on Mon Jan 31 12:54:24 2005 for spot by doxygen 1.4.0