ADMB Documentation  11.1x.2706
 All Classes Files Functions Variables Typedefs Friends Defines
timer.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: timer.cpp 2705 2014-11-20 22:56:17Z 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 #ifdef _WIN64
00085     t=GetTickCount64();
00086 #else
00087     t=GetCurrentTime();
00088 #endif
00089     told=t;
00090   }
00091 
00096   double adtimer::get_elapsed_time_and_reset(void)
00097   {
00098 #ifdef _WIN64
00099     t=GetTickCount64();
00100 #else
00101     t=GetCurrentTime();
00102 #endif
00103     //std::cout << tv.tv_sec << " " << tv.tv_usec << std::endl;
00104     double diff=t-told;
00105     told=t;
00106     return diff;
00107   }
00108 
00113   double adtimer::get_elapsed_time(void)
00114   {
00115 #ifdef _WIN64
00116     t=GetTickCount64();
00117 #else
00118     t=GetCurrentTime();
00119 #endif
00120     //std::cout << tv.tv_sec << " " << tv.tv_usec << std::endl;
00121     double diff=t-told;
00122     return diff;
00123   }
00124 #endif