HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Easy-to-use C++ class for asking current time stamp in milli-, micro- and nanoseconds

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
micronanosecondsandtimemillieasystampforcurrentasking

Problem

I have to benchmark my code often, and decided that it is about time to implement an easy API for that:

current_time.h:

#ifndef CURRENT_TIME_H
#define CURRENT_TIME_H

#include 
#include 

class CurrentTime {
    std::chrono::high_resolution_clock m_clock;

public:
    uint64_t milliseconds();
    uint64_t microseconds();
    uint64_t nanoseconds();
};

#endif  /* CURRENT_TIME_H */


current_time.cpp:

#include "current_time.h"

uint64_t CurrentTime::milliseconds() 
{
    return std::chrono::duration_cast
              (m_clock.now().time_since_epoch()).count();
}

uint64_t CurrentTime::microseconds() 
{
    return std::chrono::duration_cast
              (m_clock.now().time_since_epoch()).count();
}

uint64_t CurrentTime::nanoseconds()
{
    return std::chrono::duration_cast
              (m_clock.now().time_since_epoch()).count();
}


main.cpp:

#include 
#include "current_time.h"

using namespace std;

int main(int argc, char** argv) {
    CurrentTime current_time;

    uint64_t start1 = current_time.milliseconds();
    uint64_t start2 = current_time.microseconds();
    uint64_t start3 = current_time.nanoseconds();

    for (int i = 0; i < 100000000; ++i) {

    }

    uint64_t end1 = current_time.milliseconds();
    uint64_t end2 = current_time.microseconds();
    uint64_t end3 = current_time.nanoseconds();

    cout << "Milliseconds: "   << (end1 - start1)
         << ", microseconds: " << (end2 - start2)
         << ", nanoseconds: "  << (end3 - start3)
         << ".\n";

    return 0;
}


Any critique is much appreciated, yet I would like to hear your opinions on returning uint64_t: is there some implicit conversions going on?

Solution

Since std::chrono::high_resolution_clock::now() is static method you don't need any instance for it.

This is all you need.

#ifndef CURRENT_TIME_H
#define CURRENT_TIME_H

#include 
#include 

inline uint64_t CurrentTime_milliseconds() 
{
    return std::chrono::duration_cast
              (std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}

inline uint64_t CurrentTime_microseconds() 
{
    return std::chrono::duration_cast
              (std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}

inline uint64_t CurrentTime_nanoseconds()
{
    return std::chrono::duration_cast
              (std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}  

#endif  /* CURRENT_TIME_H */

Code Snippets

#ifndef CURRENT_TIME_H
#define CURRENT_TIME_H

#include <chrono>
#include <cstdint>

inline uint64_t CurrentTime_milliseconds() 
{
    return std::chrono::duration_cast<std::chrono::milliseconds>
              (std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}

inline uint64_t CurrentTime_microseconds() 
{
    return std::chrono::duration_cast<std::chrono::microseconds>
              (std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}

inline uint64_t CurrentTime_nanoseconds()
{
    return std::chrono::duration_cast<std::chrono::nanoseconds>
              (std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}  

#endif  /* CURRENT_TIME_H */

Context

StackExchange Code Review Q#132852, answer score: 3

Revisions (0)

No revisions yet.