ADMB Documentation  11.2.2853
 All Classes Files Functions Variables Typedefs Friends Defines
timer.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: timer.cpp 2722 2014-11-25 20:20:35Z 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 
00081 adtimer::adtimer()
00082 {
00083 #if defined(_MSC_VER)
00084   t=GetTickCount64();
00085 #else
00086   t=GetCurrentTime();
00087 #endif
00088   told=t;
00089 }
00090 
00095 double adtimer::get_elapsed_time_and_reset()
00096 {
00097   double diff = get_elapsed_time();
00098   told = t;
00099   return diff;
00100 }
00101 
00106 double adtimer::get_elapsed_time()
00107 {
00108 #if defined(_MSC_VER)
00109   t=GetTickCount64();
00110   unsigned long long diff = t - told;
00111 #else
00112   t=GetCurrentTime();
00113   DWORD diff  = t - told;
00114 #endif
00115   return static_cast<double>(diff);
00116 }
00117 #endif