00001 /* 00002 * This file: timer.cpp, is part of the Astrog software library 00003 * that uses the graphics processing unit (GPU) to accelerate 00004 * astronomy calculations. 00005 * 00006 * Copyright (C) 2007 C. J. Harris 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00021 * 02110-1301 USA 00022 */ 00023 00029 #include "libastrog_timer.h" 00030 00031 namespace astrog { 00032 00033 double Timer::grain = 0; 00034 00035 Timer::Timer() 00036 { 00037 // get grain if it hasn't been determined already 00038 if (this->get_grain()==0.0) { 00039 this->set_grain(); 00040 } 00041 // timer starts off 00042 this->timer_status = 0; 00043 // accumulated time is zero 00044 this->time_elapsed = 0.0; 00045 // zero start/stops so far 00046 this->error_grains = 0; 00047 00048 00049 00050 } 00051 00052 Timer::~Timer() 00053 { 00054 } 00055 00056 void Timer::start() 00057 { 00058 if (this->timer_status == 1) {} // TODO throw error 00059 this->timer_status = 1; 00060 this->error_grains += 1; 00061 ftime(&this->latest_start); 00062 } 00063 00064 void Timer::stop() 00065 { 00066 if (this->timer_status == 0) {} // TODO throw error 00067 this->timer_status = 0; 00068 this->error_grains += 1; 00069 ftime(&this->latest_stop); 00070 time_elapsed += ((double)(latest_stop.time-latest_start.time) 00071 +((double)(latest_stop.millitm-latest_start.millitm))/1000.0f); 00072 } 00073 00074 void Timer::reset() 00075 { 00076 if (this->timer_status == 1) { 00077 this->stop(); 00078 this->start(); 00079 } 00080 this->time_elapsed = 0; 00081 } 00082 00083 double Timer::get_time() 00084 { 00085 if (this->timer_status == 1) {} // TODO Throw error 00086 return this->time_elapsed; 00087 } 00088 00089 double Timer::get_error_absolute() 00090 { 00091 return this->error_grains*Timer::grain; 00092 } 00093 00094 double Timer::get_error_relative() 00095 { 00096 return (this->get_error_absolute())/(this->get_time()); 00097 } 00098 00099 double Timer::get_error_percentage() 00100 { 00101 return 100*(this->get_error_relative()); 00102 } 00103 00104 double Timer::get_grain () 00105 { 00106 return Timer::grain; 00107 } 00108 00109 void Timer::set_grain () 00110 { 00111 struct timeb start; 00112 struct timeb stop; 00113 ftime(&start); 00114 ftime(&stop); 00115 double time = 0; 00116 while (time<=0) { 00117 ftime(&stop); 00118 time = ((double)(stop.time-start.time)+(double)(stop.millitm-start.millitm)/1000.0f); 00119 } 00120 Timer::grain = time; 00121 } 00122 00123 } // end namespace