Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何查找其他c程序的运行时_C++_C_Time - Fatal编程技术网

C++ 如何查找其他c程序的运行时

C++ 如何查找其他c程序的运行时,c++,c,time,C++,C,Time,我想找出另一个程序运行所需的时间; 我使用以下代码 system("time ./a.out > garb"); 它的输出非常奇怪 #include <stdio.h> int main() { long int i; for ( i = 0; i < 10000000; i++ ) { printf("Hello World!\n"); } printf("C Pro

我想找出另一个程序运行所需的时间; 我使用以下代码

system("time ./a.out > garb");
它的输出非常奇怪

#include <stdio.h>

int main()
{

        long int i;

        for ( i = 0; i < 10000000; i++ ) {
                printf("Hello World!\n");
        }
        printf("C Program\n");
        return 0;
}

我想我们可以找到另一种方法来代替系统中的时间。 比如:

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

int main() 
{
    clock_t start = clock();
    system('./a.out');
    printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
    return (0);
}
#包括
#包括
int main()
{
时钟启动=时钟();
系统('./a.out');
printf(“经过的时间:%f\n”,((双)时钟()-开始)/每秒时钟数);
返回(0);
}

一种方法是使用
wait3
wait4
功能(如果系统中有这些功能)

退出子进程后,您的程序将获得该子进程的资源使用情况

struct rusage
的所有字段都不会更新,但前两个字段会告诉您需要什么:

struct rusage {
    struct timeval ru_utime; /* user CPU time used */
    struct timeval ru_stime; /* system CPU time used */
    ...
ru_-utime
ru_-stime
之和是子进程使用的总CPU时间

使用
wait3/wait4
并不像调用
system()
函数那样简单

编辑: 通过将
时间的打印输出的这两个值相加,应该可以得到相同的结果:

0.31用户0.10系统


英特尔处理器插入了一个64位寄存器,用于计算进程的时钟


在我看来,使用C的库函数或系统调用很难获得准确的结果。相反,最好使用性能API()来获得更准确的结果。更具体地说,您可以看到PAPI定义的时间。

在我看来就像GNU时间。你期待什么?POSIX格式?我想用c/c++程序计算上述程序运行的时间。为此,我运行系统(“time./a.out”)。但这并不像我们通过bash运行它时那样给出输出。这是因为bash中内置了一个版本的
time
,而在您的代码中,您调用系统中安装的程序(GNU-time)。默认情况下,bash的版本以POSIX格式输出,而GNU-time则以不同的格式输出。您可以使用代码中的
time-p
以POSIX格式将其输出。为什么不试试User1的建议呢?这只是测量墙时间,而不是例如,如果时间花在用户代码或等待某些系统资源上。不确定OP想要的是什么,因为问题不完整。是的,但他想要的不是“时间。/a.out”这篇文章包含了标签
c
。不管怎样,你能告诉我你所说的“墙时间”是什么意思吗?你能给我一些建议,如何用一个c来更容易地计算其他c程序的运行时间吗program@Shubham沙玛:也许像你那样使用system()函数更容易。并从文本中解析时间。请参阅我的编辑。
pid_t wait3(int *status, int options, struct rusage *rusage);    
pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage);
struct rusage {
    struct timeval ru_utime; /* user CPU time used */
    struct timeval ru_stime; /* system CPU time used */
    ...