由一块code的,在Unix / Linux采取在C CPU时间量测 [英] Measuring amount of CPU time taken by a piece of code, in C on Unix/Linux

查看:95
本文介绍了由一块code的,在Unix / Linux采取在C CPU时间量测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否时钟()被用来作为一个可靠的API来衡量CPU执行code的一个片段所用的时间?当验证USNG次()/时钟(),两者似乎并没有衡量CPU时间采取precisely。

Can clock() be used as a dependable API to measure time taken by CPU to execute a snippet of code? When verified usng times() / clock(), both do not seem to measure the CPU time taken precisely.

首先,可以将API的时钟()/次()一起使用,以测量code的功能/片段的执行时间,如下面的例子中给出。有没有更好的,可靠的替代方案?该机制是在Linux,HP-UX,IBM-AIX和Sun Solaris的工作,我们需要测量(安培;&安培;比较)一张code的在所有这些平台的性能。

Firstly, can the APIs clock()/times() be used to measure the execution time of a function/snippet of code, as given in the example below? Is there a better and dependable alternative? The mechanism is to work on Linux, HP-UX, IBM-AIX and Sun Solaris as we need to measure (&& compare) the performance of a piece of code on all these platforms.

请建议。另外,请让我知道如果我错过什么小事。

Kindly suggest. Also, please let me know if am missing anything trivial.

bbb@m_001:/tmp/kk1$ ./perf_clock 102400
{clock(): S          0 E          0 D    0.0000000000}
bbb@m_001:/tmp/kk1$ ./perf_clock 204800
{clock(): S          0 E      10000 D    0.0100000000}
bbb@m_001:/tmp/kk1$ cat perf_clock.c

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

void test_clock(char* sbuf, int* len){
    clock_t start, end; int i=0;
    start = clock();
    while(i++ < 500) memset((char*)sbuf,0,*len);
    end = clock();
    printf("{clock(): S %10lu E %10lu D %15.10f}\n",
        start,end,(end-start)/(double) CLOCKS_PER_SEC);
}
int main(int argc,char* argv[])
{
        int len=atoi(argv[1]);
        char *sbuf=(char*)malloc(len);
        test_clock(sbuf,&len);
        free(sbuf); return 0;
}

结果似乎说要memset的()一100 KB块,500次,没有花费的时间。抑或是说,这是不是在微秒衡量的?

The results seem to say that to memset() a 100 KB chunk, 500 times, there is no time spent. Or does it say that it is not measurable in microseconds?

其实,它不是memset的(),但另一个函数[即prepares一个巨大的结构,大小约为1 MB,mallocs这种结构的副本,并在Oracle数据库中选择和填充这些结构与DB数据],它我试图衡量。即使这说明蜱0,而这正是使我感到困惑。

Actually, it is not memset() but another function[that prepares a huge structure sized around 1MB, mallocs a copy of this structure, does an Oracle DB select and populate these structures with the data from DB] which am trying to measure. Even this shows 0 ticks, and that is what has kept me confused.

谢谢!

推荐答案

在最近的Linux的(*)。你可以从/ proc文件系统这一信息。在文件 / proc /进程/ STAT 第14项在用户态code和15日入境时的jiffies数系统$用过的jiffies数C $角

On recent Linux's (*). you can get this information from the /proc filesystem. In the file /proc/PID/stat the 14th entry has the number of jiffies used in userland code and the 15th entry has the number of jiffies used in system code.

如果你想看到在每个线程的基础数据,您应该引用文件 / proc /进程/任务/ TID / STAT 代替。

If you want to see the data on a per-thread basis, you should reference the file /proc/PID/task/TID/stat instead.

要转换的jiffies到微秒,可以使用以下内容:

To convert jiffies to microseconds, you can use the following:

define BLTPR_USEC_PER_SEC         1000000UL

long long jiffies_to_microsecond(long long jiffies)
{
    long hz = sysconf(_SC_CLK_TCK);
    if (hz <= USEC_PER_SEC && !(USEC_PER_SEC % hz))
    {
        return (USEC_PER_SEC / hz) * jiffies;
    }
    else if (hz > USEC_PER_SEC && !(hz % USEC_PER_SEC))
    {
        return (jiffies + (hz / USEC_PER_SEC) - 1) / (hz / USEC_PER_SEC);
    }
    else
    {
        return (jiffies * USEC_PER_SEC) / hz;
    }
}

如果你关心的是每个进程的统计数据,的getrusage 更容易。但是,如果你想成为ppared做到这一点在每个线程的基础$ P $,这种技术是其他更好的然后将文件名,code将是让每个进程或每个线程的数据相同

If all you care about is the per-process statistics, getrusage is easier. But if you want to be prepared to do this on a per-thread basis, this technique is better as other then the file name, the code would be identical for getting the data per-process or per-thread.

* - 我不知道什么时候引入了统计文件。您需要确认您的系统吧。

* - I'm not sure exactly when the stat file was introduced. You will need to verify your system has it.

这篇关于由一块code的,在Unix / Linux采取在C CPU时间量测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆