代码之家  ›  专栏  ›  技术社区  ›  radman

获取系统时间和用户时间

  •  1
  • radman  · 技术社区  · 14 年前

    我在linux上工作,使用/proc//stat中的值来记录性能数据。我目前正在记录进程用户时间和进程系统时间。我遇到的问题是,代码在ubuntu10.04上给出了正确的值,但在fedora13下似乎是错误的。

    ps 但我能看到的唯一选择是“过时的bsd排序方法”,我无法使用它。

    或者,有一种更好的方法通过C++函数来提取性能值,而这些API值是我不知道的?

    3 回复  |  直到 14 年前
        1
  •  2
  •   caf    14 年前

    如果您想从进程本身(或父进程)了解CPU时间,可以使用 times()

    clock_t times(struct tms *buf);
    
    struct tms {
        clock_t tms_utime;  /* user time */
        clock_t tms_stime;  /* system time */
        clock_t tms_cutime; /* user time of children */
        clock_t tms_cstime; /* system time of children */
    };
    

        2
  •  1
  •   Bowie Owens    14 年前

    除了caf建议的times()之外,您还可以从getrusage()获得更详细的信息。

    http://www.kernel.org/doc/man-pages/online/pages/man2/getrusage.2.html

        3
  •  0
  •   Guillaume Massé    14 年前

    time ./program