1ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov/*
2ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * The copyright in this software is being made available under the 2-clauses
3ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * BSD License, included below. This software may be subject to other third
4ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * party and contributor rights, including patent rights, and no such rights
5ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * are granted under this license.
6ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov *
7ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * Copyright (c) 2005, Herve Drolon, FreeImage Team
8ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * All rights reserved.
9ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov *
10ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * Redistribution and use in source and binary forms, with or without
11ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * modification, are permitted provided that the following conditions
12ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * are met:
13ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * 1. Redistributions of source code must retain the above copyright
14ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov *    notice, this list of conditions and the following disclaimer.
15ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * 2. Redistributions in binary form must reproduce the above copyright
16ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov *    notice, this list of conditions and the following disclaimer in the
17ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov *    documentation and/or other materials provided with the distribution.
18ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov *
19ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * POSSIBILITY OF SUCH DAMAGE.
30ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov */
31ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov
32ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov#ifdef _WIN32
33ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov#include <windows.h>
34ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov#else
35ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov#include <sys/time.h>
36ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov#include <sys/resource.h>
37ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov#include <sys/times.h>
38ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov#endif /* _WIN32 */
39ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov#include "opj_includes.h"
40ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov
41ee451cb395940862dad63c85adfe8f2fd55e864cSvet GanovOPJ_FLOAT64 opj_clock(void) {
42ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov#ifdef _WIN32
43ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	/* _WIN32: use QueryPerformance (very accurate) */
44ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov    LARGE_INTEGER freq , t ;
45ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov    /* freq is the clock speed of the CPU */
46ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov    QueryPerformanceFrequency(&freq) ;
47ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	/* cout << "freq = " << ((double) freq.QuadPart) << endl; */
48ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov    /* t is the high resolution performance counter (see MSDN) */
49ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov    QueryPerformanceCounter ( & t ) ;
50ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov    return ( t.QuadPart /(OPJ_FLOAT64) freq.QuadPart ) ;
51ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov#else
52ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	/* Unix or Linux: use resource usage */
53ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov    struct rusage t;
54ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov    OPJ_FLOAT64 procTime;
55ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov    /* (1) Get the rusage data structure at this moment (man getrusage) */
56ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov    getrusage(0,&t);
57ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov    /* (2) What is the elapsed time ? - CPU time = User time + System time */
58ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	/* (2a) Get the seconds */
59ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov    procTime = (OPJ_FLOAT64)(t.ru_utime.tv_sec + t.ru_stime.tv_sec);
60ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov    /* (2b) More precisely! Get the microseconds part ! */
61ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov    return ( procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) * 1e-6 ) ;
62ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov#endif
63ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov}
64ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov
65