summaryrefslogtreecommitdiff
path: root/Source/ThirdParty/ANGLE/util/linux/LinuxTimer.cpp
blob: 1968205808887946b2b0a9aae935c7dba7e52900 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// Copyright (c) 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

// LinuxTimer.cpp: Implementation of a high precision timer class on Linux

#include "linux/LinuxTimer.h"
#include <iostream>

LinuxTimer::LinuxTimer()
    : mRunning(false)
{
}

void LinuxTimer::start()
{
    clock_gettime(CLOCK_MONOTONIC, &mStartTime);
    mRunning = true;
}

void LinuxTimer::stop()
{
    clock_gettime(CLOCK_MONOTONIC, &mStopTime);
    mRunning = false;
}

double LinuxTimer::getElapsedTime() const
{
    struct timespec endTime;
    if (mRunning)
    {
        clock_gettime(CLOCK_MONOTONIC, &endTime);
    }
    else
    {
        endTime = mStopTime;
    }

    double startSeconds = mStartTime.tv_sec + (1.0 / 1000000000) * mStartTime.tv_nsec;
    double endSeconds = endTime.tv_sec + (1.0 / 1000000000) * endTime.tv_nsec;
    return endSeconds - startSeconds;
}

Timer *CreateTimer()
{
    return new LinuxTimer();
}