1 //------------------------------------------------------------------------------ 2 // GraphBLAS/Demo/Include/simple_timer.h: a timer for performance measurements 3 //------------------------------------------------------------------------------ 4 5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved. 6 // SPDX-License-Identifier: Apache-2.0 7 8 //------------------------------------------------------------------------------ 9 10 /* 11 There is no method that works on all operating systems for finding the 12 current time with high resolution that is suitable for performance 13 measurements. The simple_timer.c and simple_timer.h functions provide a 14 portable alternative. 15 16 simple_tic (tic) ; gets the current time and saves it in tic [0..1]. 17 18 t = simple_toc (tic) ; returns the time in seconds since the last call to 19 simple_toc, as a single double value. 20 21 Usage: 22 23 #include "simple_timer.h" 24 double tic [2], r, s, t ; 25 26 simple_tic (tic) ; // start the timer 27 // do some work A 28 t = simple_toc (tic) ; // t is time for work A, in seconds 29 // do some work B 30 s = simple_toc (tic) ; // s is time for work A and B, in seconds 31 32 simple_tic (tic) ; // restart the timer 33 // do some work C 34 r = simple_toc (tic) ; // r is time for work C, in seconds 35 */ 36 37 #ifndef SIMPLE_TIMER_H 38 #define SIMPLE_TIMER_H 39 40 #ifndef GB_PUBLIC 41 // Exporting/importing symbols for Microsoft Visual Studio 42 #if ( _MSC_VER && !__INTEL_COMPILER ) 43 #ifdef GB_LIBRARY 44 // compiling SuiteSparse:GraphBLAS itself, exporting symbols to user apps 45 #define GB_PUBLIC extern __declspec ( dllexport ) 46 #else 47 // compiling the user application, importing symbols from SuiteSparse:GraphBLAS 48 #define GB_PUBLIC extern __declspec ( dllimport ) 49 #endif 50 #else 51 // for other compilers 52 #define GB_PUBLIC extern 53 #endif 54 #endif 55 56 #include <time.h> 57 58 //------------------------------------------------------------------------------ 59 // decide which timer to use 60 //------------------------------------------------------------------------------ 61 62 #if defined ( _OPENMP ) 63 64 // if OpenMP is available, use omp_get_wtime 65 #include <omp.h> 66 67 #elif defined ( __linux__ ) || defined ( __GNU__ ) 68 69 // otherwise, on Linux/GNU, use clock_gettime. May require -lrt 70 #include <sys/time.h> 71 72 #elif defined ( __MACH__ ) && defined ( __APPLE__ ) 73 74 // otherwise, on the Mac, use the MACH timer 75 #include <mach/clock.h> 76 #include <mach/mach.h> 77 78 #else 79 80 // Finally, the ANSI C11 clock() function is used if no other timer 81 // is available. 82 83 #endif 84 85 //------------------------------------------------------------------------------ 86 87 GB_PUBLIC 88 void simple_tic // returns current time in seconds and nanoseconds 89 ( 90 double tic [2] // tic [0]: seconds, tic [1]: nanoseconds 91 ) ; 92 93 GB_PUBLIC 94 double simple_toc // returns time since last simple_tic 95 ( 96 const double tic [2] // tic from last call to simple_tic 97 ) ; 98 99 #endif 100 101