ADMB Documentation  11.1.2402
 All Classes Files Functions Variables Typedefs Friends Defines
timer.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: timer.cpp 2312 2014-09-09 23:59:40Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include <admodel.h>
00012 #if !defined(_WIN32)
00013 #include <sys/time.h>
00014 #include <iostream>
00015 
00019 adtimer::adtimer (void)
00020 {
00021   gettimeofday(&tv,0);
00022 
00023   tvold.tv_sec=tv.tv_sec;
00024   tvold.tv_usec=tv.tv_usec;
00025 }
00026 
00031 double adtimer::get_elapsed_time_and_reset(void)
00032 {
00033   long int nsec=0;
00034   long int nusec=0;
00035   gettimeofday(&tv,0);
00036   if ( tv.tv_usec< tvold.tv_usec)
00037   {
00038     nsec=tv.tv_sec-tvold.tv_sec-1;
00039     nusec=tv.tv_usec-tvold.tv_usec+1000000;
00040   }
00041   else
00042   {
00043     nsec=tv.tv_sec-tvold.tv_sec;
00044     nusec=tv.tv_usec-tvold.tv_usec;
00045   }
00046 
00047   //reset
00048   tvold.tv_sec=tv.tv_sec;
00049   tvold.tv_usec=tv.tv_usec;
00050 
00051   return 1000.0 * (double)nsec + 0.001 * (double)nusec;
00052 }
00053 
00057 double adtimer::get_elapsed_time(void)
00058 {
00059   long int nsec=0;
00060   long int nusec=0;
00061   gettimeofday(&tv,0);
00062   if ( tv.tv_usec< tvold.tv_usec)
00063   {
00064     nsec=tv.tv_sec-tvold.tv_sec-1;
00065     nusec=tv.tv_usec-tvold.tv_usec+1000000;
00066   }
00067   else
00068   {
00069     nsec=tv.tv_sec-tvold.tv_sec;
00070     nusec=tv.tv_usec-tvold.tv_usec;
00071   }
00072   return 1000.0 * (double)nsec + 0.001 * (double)nusec;
00073 }
00074 
00075 #else
00076 #include <windows.h>
00077 
00082   adtimer::adtimer (void)
00083   {
00084     t=GetCurrentTime();
00085     told=t;
00086   }
00087 
00092   double adtimer::get_elapsed_time_and_reset(void)
00093   {
00094     t=GetCurrentTime();
00095     //std::cout << tv.tv_sec << " " << tv.tv_usec << std::endl;
00096     double diff=t-told;
00097     told=t;
00098     return diff;
00099   }
00100 
00105   double adtimer::get_elapsed_time(void)
00106   {
00107     t=GetCurrentTime();
00108     //std::cout << tv.tv_sec << " " << tv.tv_usec << std::endl;
00109     double diff=t-told;
00110     return diff;
00111   }
00112 #endif