Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

pctimer.c

Go to the documentation of this file.
00001 /* Win32 performance counter functions to get a high-resolution timer
00002  *
00003  * By Wu Yongwei
00004  *
00005  */
00006 
00007 #include <stdio.h>
00008 #include <pctimer.h>
00009 
00010 #ifdef __MINGW32__
00011 #include <windows.h>
00012 #include <winsock2.h>
00013 /* Broken version just for windows */
00014 int gettimeofday(struct timeval *tv, void *tz);
00015 #else
00016 #define HAVE_GETTIMEOFDAY
00017 #endif
00018 
00019 #ifndef __GNUC__
00020 #define EPOCHFILETIME (116444736000000000i64)
00021 #else
00022 #define EPOCHFILETIME (116444736000000000LL)
00023 #endif
00024 
00025 
00026 #include <sys/time.h>
00027 
00028 
00029 pctimer_t pctimer(void)
00030 {
00031     struct timeval tv;
00032     gettimeofday(&tv, NULL);
00033     return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
00034 }
00035 
00036 /*
00037  * Defines gettimeofday
00038  *
00039  * Based on timeval.h by Wu Yongwei
00040  *
00041  * This library is free software; you can redistribute it and/or
00042  * modify it under the terms of the GNU Lesser General Public
00043  * License as published by the Free Software Foundation; either
00044  * version 2.1 of the License, or (at your option) any later version.
00045  *
00046  * This library is distributed in the hope that it will be useful,
00047  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00048  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00049  * Lesser General Public License for more details.
00050  *
00051  * You should have received a copy of the GNU Lesser General Public
00052  * License along with this library; if not, write to the Free Software
00053  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00054  */
00055 
00056 #ifndef HAVE_GETTIMEOFDAY
00057 int gettimeofday(struct timeval *tv, void *tz)
00058 {
00059     FILETIME        ft;
00060     LARGE_INTEGER   li;
00061     __int64         t;
00062 
00063     if (tv)
00064     {
00065         GetSystemTimeAsFileTime(&ft);
00066         li.LowPart  = ft.dwLowDateTime;
00067         li.HighPart = ft.dwHighDateTime;
00068         t  = li.QuadPart;       /* In 100-nanosecond intervals */
00069         t -= EPOCHFILETIME;     /* Offset to the Epoch time */
00070         t /= 10;                /* In microseconds */
00071         tv->tv_sec  = (long)(t / 1000000);
00072         tv->tv_usec = (long)(t % 1000000);
00073     }
00074     return 0;
00075 }
00076 
00077 #endif /* HAVE_GETTIMEOFDAY */
00078 

Generated on Tue Feb 8 00:05:17 2005 for Neuroserver by doxygen 1.3.3