opj_decompress: use clock_gettime() instead of getrusage() so as to get the time...
authorEven Rouault <even.rouault@spatialys.com>
Wed, 25 May 2016 19:39:21 +0000 (21:39 +0200)
committerEven Rouault <even.rouault@spatialys.com>
Wed, 25 May 2016 19:59:43 +0000 (21:59 +0200)
src/bin/jp2/CMakeLists.txt
src/bin/jp2/opj_decompress.c

index dc013c21735b883842a51248989cdc3751d9a79f..ad7bce719367d85d3b3819ce932a92e3d8841b9f 100644 (file)
@@ -57,6 +57,9 @@ foreach(exe opj_decompress opj_compress opj_dump)
   # On unix you need to link to the math library:
   if(UNIX)
     target_link_libraries(${exe} m)
+    IF("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
+      target_link_libraries(${exe} rt)
+    endif()
   endif()
   # Install exe
   install(TARGETS ${exe}
index 0e02c56b686af1981ddc7707a06563411157369e..57fe554be9e37a68fac6c7c22c30a33d07d04634 100644 (file)
@@ -43,6 +43,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <math.h>
+#include <time.h>
 
 #ifdef _WIN32
 #include "windirent.h"
@@ -907,17 +908,22 @@ OPJ_FLOAT64 opj_clock(void) {
     /* t is the high resolution performance counter (see MSDN) */
     QueryPerformanceCounter ( & t ) ;
        return freq.QuadPart ? (t.QuadPart / (OPJ_FLOAT64)freq.QuadPart) : 0;
+#elif defined(__linux)
+       struct timespec ts;
+       clock_gettime(CLOCK_REALTIME, &ts);
+       return( ts.tv_sec + ts.tv_nsec * 1e-9 );
 #else
-       /* Unix or Linux: use resource usage */
-    struct rusage t;
-    OPJ_FLOAT64 procTime;
-    /* (1) Get the rusage data structure at this moment (man getrusage) */
-    getrusage(0,&t);
-    /* (2) What is the elapsed time ? - CPU time = User time + System time */
+       /* Unix : use resource usage */
+       /* FIXME: this counts the total CPU time, instead of the user perceived time */
+       struct rusage t;
+       OPJ_FLOAT64 procTime;
+       /* (1) Get the rusage data structure at this moment (man getrusage) */
+       getrusage(0,&t);
+       /* (2) What is the elapsed time ? - CPU time = User time + System time */
        /* (2a) Get the seconds */
-    procTime = (OPJ_FLOAT64)(t.ru_utime.tv_sec + t.ru_stime.tv_sec);
-    /* (2b) More precisely! Get the microseconds part ! */
-    return ( procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) * 1e-6 ) ;
+       procTime = (OPJ_FLOAT64)(t.ru_utime.tv_sec + t.ru_stime.tv_sec);
+       /* (2b) More precisely! Get the microseconds part ! */
+       return ( procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) * 1e-6 ) ;
 #endif
 }