Savarese Software Research
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

StopWatch.h

Go to the documentation of this file.
00001 /*
00002  * $Id: StopWatch.h 5870 2005-10-28 02:08:08Z dfs $
00003  *
00004  * Copyright 2003-2005 Daniel F. Savarese
00005  * Copyright 2005 Savarese Software Research
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     https://www.savarese.com/software/ApacheLicense-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 
00026 #ifndef __SAVA_TIME_STOPWATCH_H
00027 #define __SAVA_TIME_STOPWATCH_H
00028 
00029 #include <sys/time.h>
00030 #include <sys/types.h>
00031 #include <unistd.h>
00032 
00033 #include <libsava/packages.h>
00034 
00035 __BEGIN_PACKAGE_SAVA_TIME
00036 
00044 class StopWatch {
00045   double startTime, endTime;
00046 
00047 public:
00048 
00052   StopWatch() : startTime(0.0), endTime(0.0) { }
00053 
00057   void start() {
00058     struct timeval t;
00059     struct timezone tz;
00060 
00061     gettimeofday(&t, &tz);
00062     startTime =
00063       static_cast<double>(t.tv_sec) + static_cast<double>(t.tv_usec)/1e6;
00064   }
00065 
00069   void stop() {
00070     struct timeval t;
00071     struct timezone tz;
00072 
00073     gettimeofday(&t, &tz);
00074     endTime =
00075       static_cast<double>(t.tv_sec) + static_cast<double>(t.tv_usec)/1e6;
00076   }
00077 
00083   double elapsed() {
00084     return (endTime - startTime);
00085   }
00086 };
00087 
00088 __END_PACKAGE_SAVA_TIME
00089 
00090 #endif

Savarese Software Research
Copyright © 2003-2005 Savarese Software Research and Daniel F. Savarese. All rights reserved.