ADMB Documentation  11.1.2274
 All Classes Files Functions Variables Typedefs Friends Defines
timer.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: timer.cpp 1906 2014-04-17 20:52:49Z 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 
00020   adtimer::adtimer (void)
00021   {
00022     gettimeofday(&tv,0);
00023 
00024     tvold.tv_sec=tv.tv_sec;
00025     tvold.tv_usec=tv.tv_usec;
00026   }
00027 
00032   double adtimer::get_elapsed_time_and_reset(void)
00033   {
00034     gettimeofday(&tv,0);
00035     //std::cout << tv.tv_sec << " " << tv.tv_usec << std::endl;
00036     int nsec=0;
00037     int nusec=0;
00038     if ( tv.tv_usec< tvold.tv_usec)
00039     {
00040       nsec=tv.tv_sec-tvold.tv_sec-1;
00041       nusec=tv.tv_usec-tvold.tv_usec+1000000;
00042     }
00043     else
00044     {
00045       nsec=tv.tv_sec-tvold.tv_sec;
00046       nusec=tv.tv_usec-tvold.tv_usec;
00047     }
00048     tvold.tv_sec=tv.tv_sec;
00049     tvold.tv_usec=tv.tv_usec;
00050     return 1000.*nsec+nusec/1000.;
00051   }
00052 
00056   double adtimer::get_elapsed_time(void)
00057   {
00058     int nsec=0;
00059     int nusec=0;
00060     gettimeofday(&tv,0);
00061     //std::cout << tv.tv_sec << " " << tv.tv_usec << std::endl;
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.*nsec+nusec/1000.;
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