문제

나는 내 프로그램에서 "rusage" 통계를 사용하여 다음과 유사한 데이터를 얻으려고 합니다. 시간 도구.그러나 나는 내가 뭔가 잘못하고 있다고 확신합니다.값은 대략 맞는 것 같지만 때때로 약간 이상할 수 있습니다.온라인에서 좋은 자료를 찾지 못했습니다.누군가 그것을 더 잘하는 방법을 알고 있습니까?

코드가 길어서 죄송합니다.

class StopWatch {
public:
    void start() {
        getrusage(RUSAGE_SELF, &m_begin);
        gettimeofday(&m_tmbegin, 0);
    }

    void stop() {
        getrusage(RUSAGE_SELF, &m_end);
        gettimeofday(&m_tmend, 0);
        timeval_sub(m_end.ru_utime, m_begin.ru_utime, m_diff.ru_utime);
        timeval_sub(m_end.ru_stime, m_begin.ru_stime, m_diff.ru_stime);
        timeval_sub(m_tmend, m_tmbegin, m_tmdiff);
    }

    void printf(std::ostream& out) const {
        using namespace std;

        timeval const& utime = m_diff.ru_utime;
        timeval const& stime = m_diff.ru_stime;

        format_time(out, utime);
        out << "u ";
        format_time(out, stime);
        out << "s ";
        format_time(out, m_tmdiff);
    }

private:
    rusage m_begin;
    rusage m_end;
    rusage m_diff;
    timeval m_tmbegin;
    timeval m_tmend;
    timeval m_tmdiff;

    static void timeval_add(timeval const& a, timeval const& b, timeval& ret) {
        ret.tv_usec = a.tv_usec + b.tv_usec;
        ret.tv_sec = a.tv_sec + b.tv_sec;
        if (ret.tv_usec > 999999) {
            ret.tv_usec -= 1000000;
            ++ret.tv_sec;
        }
    }

    static void timeval_sub(timeval const& a, timeval const& b, timeval& ret) {
        ret.tv_usec = a.tv_usec - b.tv_usec;
        ret.tv_sec = a.tv_sec - b.tv_sec;
        if (a.tv_usec < b.tv_usec) {
            ret.tv_usec += 1000000;
            --ret.tv_sec;
        }
    }

    static void format_time(std::ostream& out, timeval const& tv) {
        using namespace std;
        long usec = tv.tv_usec;
        while (usec >= 1000)
            usec /= 10;
        out << tv.tv_sec << '.' << setw(3) << setfill('0') << usec;
    }
}; // class StopWatch
도움이 되었습니까?

해결책

목적은 무엇입니까:

while (usec >= 1000)
    usec /= 10;

나는 당신이 usec의 가장 중요한 세 자리 숫자를 원한다고 생각합니다.이 경우 제가 생각할 수 있는 가장 간단한 방법은 usec를 1000으로 나누고 끝내는 것입니다.

테스트 사례:

  • 999999 ⇒ 999
  • 99999 ⇒ 999 (099여야 함)
  • 9999 ⇒ 999 (009여야 함)
  • 999 ⇒ 999 (000이어야 함)

다른 팁

아마도 sec와 usec 구성에 버그가 있는 것 같습니다.나는 당신이 보고 있는 오류의 종류를 알지 못하면 정확히 무엇을 말할 수 없습니다.대략적인 추측은 usec가 999999보다 클 수 없다는 것입니다. 따라서 sec를 조정할 시기를 알기 위해 오버플로에 의존하고 있습니다.기간 출력 형식에 문제가 있을 수도 있습니다.

그래도.출력에 대한 자신만의 방식을 구축하려고 시도하는 대신 utime 및 stime 구성 요소를 부동 초로 저장하는 것은 어떻습니까?나는 다음이 당신에게 적절한 초를 줄 것이라고 확신합니다.

static int timeval_diff_ms(timeval const& end, timeval const& start) {
    int micro_seconds = (end.tv_sec  - start.tv_sec) * 1000000 
        + end.tv_usec - start.tv_usec;

    return micro_seconds;
}

static float timeval_diff(timeval const& end, timeval const& start) {
    return (timeval_diff_ms(end, start)/1000000.0f);
}

이것을 다시 루세지로 분해하려면 언제든지 int-div 및 모듈로를 사용할 수 있습니다.

@크리스:

나는 당신이 usec의 가장 중요한 세 자리 숫자를 원한다고 생각합니다

예.의 전체 자릿수 usec 다양하므로 1000 미만으로 줄이고 싶습니다.예를 들어, usec=1000, 나는 결과 100을 얻고 싶습니다 (당신이 제안한대로 1이 아님).그러므로 단순히 1000으로 나눌 수는 없습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top