Skip to main content
deleted 23 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Time spend in C++-Scopescoped timer

I want to check some timing of my (pre-C11)-c++ C++ and wrote a class that looks like:

class ScopedTimer {
    timespec start_,end_;
    std::string name_;      /// name of this timer (used in output)
    bool active_;           /// if not active, no output is printed
    clockid_t clock_id_;    /// clock type used for this timer

public:
    ScopedTimer(std::string name, clockid_t clock_id = CLOCK_REALTIME, bool active = true):
        name_(name),
        active_(active),
        clock_id_(clock_id)
    {
        clock_gettime(clock_id_, &start_);
    }

    ~ScopedTimer(){
        if (active_){
            clock_gettime(clock_id_, &end_);

            double dt_ms = (end_.tv_sec - start_.tv_sec) * 1000.0 + (end_.tv_nsec - start_.tv_nsec) / 1000000.0;

             cout << "runtime " << name_ << "  " << dt_ms  << "  ms" << endl;
        }
    }

};

ThisIt is rather easy to check the running time of a full function:

void foo()
{
 ScopedTimer t("foo");
  do_stuff();
}

or also to check a part of a function:

void findCorners(cv::Mat& img, bool with_timing_output)
{
  cv::Mat gx_dil;
  cv::Mat dil_element_x = createDilElement(1);
    {
     ScopedTimer t("dilation during findCorners",with_timing_output);
     cv::dilate(gx_img, gx_dil, dil_element_x, Point(-1, -1), 5, BORDER_DEFAULT);
    }
}

What do you think about it?

Time spend in C++-Scope

I want to check some timing of my (pre-C11)-c++ and wrote a class that looks like:

class ScopedTimer {
    timespec start_,end_;
    std::string name_;      /// name of this timer (used in output)
    bool active_;           /// if not active, no output is printed
    clockid_t clock_id_;    /// clock type used for this timer

public:
    ScopedTimer(std::string name, clockid_t clock_id = CLOCK_REALTIME, bool active = true):
        name_(name),
        active_(active),
        clock_id_(clock_id)
    {
        clock_gettime(clock_id_, &start_);
    }

    ~ScopedTimer(){
        if (active_){
            clock_gettime(clock_id_, &end_);

            double dt_ms = (end_.tv_sec - start_.tv_sec) * 1000.0 + (end_.tv_nsec - start_.tv_nsec) / 1000000.0;

             cout << "runtime " << name_ << "  " << dt_ms  << "  ms" << endl;
        }
    }

};

This is rather easy to check the running time of a full function:

void foo()
{
 ScopedTimer t("foo");
  do_stuff();
}

or also to check a part of a function:

void findCorners(cv::Mat& img, bool with_timing_output)
{
  cv::Mat gx_dil;
  cv::Mat dil_element_x = createDilElement(1);
    {
     ScopedTimer t("dilation during findCorners",with_timing_output);
     cv::dilate(gx_img, gx_dil, dil_element_x, Point(-1, -1), 5, BORDER_DEFAULT);
    }
}

What do you think about it?

Time spend in scoped timer

I want to check some timing of my (pre-C11) C++ and wrote a class:

class ScopedTimer {
    timespec start_,end_;
    std::string name_;      /// name of this timer (used in output)
    bool active_;           /// if not active, no output is printed
    clockid_t clock_id_;    /// clock type used for this timer

public:
    ScopedTimer(std::string name, clockid_t clock_id = CLOCK_REALTIME, bool active = true):
        name_(name),
        active_(active),
        clock_id_(clock_id)
    {
        clock_gettime(clock_id_, &start_);
    }

    ~ScopedTimer(){
        if (active_){
            clock_gettime(clock_id_, &end_);

            double dt_ms = (end_.tv_sec - start_.tv_sec) * 1000.0 + (end_.tv_nsec - start_.tv_nsec) / 1000000.0;

             cout << "runtime " << name_ << "  " << dt_ms  << "  ms" << endl;
        }
    }

};

It is rather easy to check the running time of a full function:

void foo()
{
 ScopedTimer t("foo");
  do_stuff();
}

or also to check a part of a function:

void findCorners(cv::Mat& img, bool with_timing_output)
{
  cv::Mat gx_dil;
  cv::Mat dil_element_x = createDilElement(1);
    {
     ScopedTimer t("dilation during findCorners",with_timing_output);
     cv::dilate(gx_img, gx_dil, dil_element_x, Point(-1, -1), 5, BORDER_DEFAULT);
    }
}

What do you think about it?

Source Link
FooBar
  • 193
  • 11

Time spend in C++-Scope

I want to check some timing of my (pre-C11)-c++ and wrote a class that looks like:

class ScopedTimer {
    timespec start_,end_;
    std::string name_;      /// name of this timer (used in output)
    bool active_;           /// if not active, no output is printed
    clockid_t clock_id_;    /// clock type used for this timer

public:
    ScopedTimer(std::string name, clockid_t clock_id = CLOCK_REALTIME, bool active = true):
        name_(name),
        active_(active),
        clock_id_(clock_id)
    {
        clock_gettime(clock_id_, &start_);
    }

    ~ScopedTimer(){
        if (active_){
            clock_gettime(clock_id_, &end_);

            double dt_ms = (end_.tv_sec - start_.tv_sec) * 1000.0 + (end_.tv_nsec - start_.tv_nsec) / 1000000.0;

             cout << "runtime " << name_ << "  " << dt_ms  << "  ms" << endl;
        }
    }

};

This is rather easy to check the running time of a full function:

void foo()
{
 ScopedTimer t("foo");
  do_stuff();
}

or also to check a part of a function:

void findCorners(cv::Mat& img, bool with_timing_output)
{
  cv::Mat gx_dil;
  cv::Mat dil_element_x = createDilElement(1);
    {
     ScopedTimer t("dilation during findCorners",with_timing_output);
     cv::dilate(gx_img, gx_dil, dil_element_x, Point(-1, -1), 5, BORDER_DEFAULT);
    }
}

What do you think about it?