Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic_timer.h and chrono_types.h #593

Merged
merged 2 commits into from Jun 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions autowiring/basic_timer.h
@@ -0,0 +1,25 @@
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
#pragma once
#include <chrono>

template<class clock_t, class duration_t = typename clock_t::duration>
class basic_timer {
public:
basic_timer(const typename clock_t::time_point& initial = clock_t::now()) : m_start(initial) {}

//A convenient method to avoid having to use duration_cast all the time.
duration_t now() const { return std::chrono::duration_cast<duration_t>(clock_t::now().time_since_epoch()); }
duration_t elapsed() const { return std::chrono::duration_cast<duration_t>(clock_t::now() - m_start); }
typename clock_t::time_point start_time() const { return m_start; }

void start() { m_start = clock_t::now(); }
duration_t mark() {
const auto now = clock_t::now();
const auto oldStart = m_start;
m_start = now;
return std::chrono::duration_cast<duration_t>(now - oldStart);
}
private:
typename clock_t::time_point m_start;
};

21 changes: 21 additions & 0 deletions autowiring/chrono_types.h
@@ -0,0 +1,21 @@
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
#pragma once
#include <autowiring/C++11/cpp11.h>
#include CHRONO_HEADER
#include "chrono_timer.h"

//Common chrono types
typedef std::chrono::duration<double> seconds_d;
typedef std::chrono::duration<double, std::milli> milliseconds_d;
typedef std::chrono::duration<double, std::micro> microseconds_d;
typedef std::chrono::duration<double, std::nano> nanoseconds_d;

typedef std::chrono::duration<float> seconds_f;
typedef std::chrono::duration<float, std::milli> milliseconds_f;
typedef std::chrono::duration<float, std::micro> microseconds_f;
typedef std::chrono::duration<float, std::nano> nanoseconds_f;

typedef basic_timer<std::chrono::profiling_clock> profiling_timer;
typedef basic_timer<std::chrono::profiling_clock, seconds_d> profiling_timer_seconds;
typedef basic_timer<std::chrono::profiling_clock, milliseconds_d> profiling_timer_milliseconds;
typedef basic_timer<std::chrono::profiling_clock, microseconds_d> profiling_timer_microseconds;