mercredi 23 décembre 2015

'Segment fault' when calculating the elapsed time using std::chrono

I changed this code and have written this function:

#include <ctime>
#include <iomanip>
#include <iostream>
#include <chrono>

#define TIMER(name) Timer timer__(name);
class Timer
{
public:

    Timer(const std::string& name) :
            name_(name), start_(std::chrono::system_clock::now())
    {
    }

    ~Timer()
    {
        auto duration = std::chrono::system_clock::now() - start_;
        std::cout << std::setw(90) << std::left << name_ << ": " <<  std::chrono::duration_cast<std::chrono::seconds>(duration).count() << "s" << std::endl;
    }
private:
    std::string name_ = 0;
    std::chrono::time_point<std::chrono::system_clock> start_ ;
};

The problem is that sometimes I get segment fault.

usage:

put something like this in the main():

TIMER("Total time");

I compiled the program with gcc version 5.2.1.

Aucun commentaire:

Enregistrer un commentaire