MFEM v2.0
tic_toc.cpp
Go to the documentation of this file.
00001 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
00002 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
00003 // reserved. See file COPYRIGHT for details.
00004 //
00005 // This file is part of the MFEM library. For more information and source code
00006 // availability see http://mfem.googlecode.com.
00007 //
00008 // MFEM is free software; you can redistribute it and/or modify it under the
00009 // terms of the GNU Lesser General Public License (as published by the Free
00010 // Software Foundation) version 2.1 dated February 1999.
00011 
00012 #include <limits.h>
00013 #include <sys/times.h>
00014 #include <unistd.h>
00015 
00016 #include "tic_toc.hpp"
00017 
00018 StopWatch::StopWatch()
00019 {
00020    my_CLK_TCK = sysconf(_SC_CLK_TCK);
00021    real_time = user_time = syst_time = 0;
00022    Running = 0;
00023 }
00024 
00025 void StopWatch::Current(clock_t *r, clock_t *u, clock_t *s)
00026 {
00027    struct tms my_tms;
00028 
00029    *r = times(&my_tms);
00030    *u = my_tms.tms_utime;
00031    *s = my_tms.tms_stime;
00032 }
00033 
00034 void StopWatch::Clear()
00035 {
00036    real_time = user_time = syst_time = 0;
00037    if (Running)
00038       Current(&start_rtime, &start_utime, &start_stime);
00039 }
00040 
00041 void StopWatch::Start()
00042 {
00043    if (Running) return;
00044    Current(&start_rtime, &start_utime, &start_stime);
00045    Running = 1;
00046 }
00047 
00048 void StopWatch::Stop()
00049 {
00050    clock_t curr_rtime, curr_utime, curr_stime;
00051 
00052    if (!Running) return;
00053    Current(&curr_rtime, &curr_utime, &curr_stime);
00054    real_time += ( curr_rtime - start_rtime );
00055    user_time += ( curr_utime - start_utime );
00056    syst_time += ( curr_stime - start_stime );
00057    Running = 0;
00058 }
00059 
00060 double StopWatch::RealTime()
00061 {
00062    clock_t curr_rtime, curr_utime, curr_stime;
00063    clock_t rtime = real_time;
00064 
00065    if (Running)
00066    {
00067       Current(&curr_rtime, &curr_utime, &curr_stime);
00068       rtime += (curr_rtime - start_rtime);
00069    }
00070 
00071    return (double)(rtime) / my_CLK_TCK;
00072 }
00073 
00074 double StopWatch::UserTime()
00075 {
00076    clock_t curr_rtime, curr_utime, curr_stime;
00077    clock_t utime = user_time;
00078 
00079    if (Running)
00080    {
00081       Current(&curr_rtime, &curr_utime, &curr_stime);
00082       utime += (curr_utime - start_utime);
00083    }
00084 
00085    return (double)(utime) / my_CLK_TCK;
00086 }
00087 
00088 double StopWatch::SystTime()
00089 {
00090    clock_t curr_rtime, curr_utime, curr_stime;
00091    clock_t stime = syst_time;
00092 
00093    if (Running)
00094    {
00095       Current(&curr_rtime, &curr_utime, &curr_stime);
00096       stime += (curr_stime - start_stime);
00097    }
00098 
00099    return (double)(stime) / my_CLK_TCK;
00100 }
00101 
00102 
00103 StopWatch tic_toc;
00104 
00105 void tic()
00106 {
00107    tic_toc.Clear();
00108    tic_toc.Start();
00109 }
00110 
00111 double toc()
00112 {
00113    return tic_toc.UserTime();
00114 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines