如何在Linux Shell上查找命令/程序占用的时间?

我们已经讨论了一种解决问题的方法 通过C库查找函数占用的时间 .如果我们在Linux上,那么很容易找到程序/命令占用的时间。

null

我们可以使用 时间 为了这个目的。所用时间以三种形式显示。 真实的: 程序结束所需的总时间 用户: 在用户模式下花费的时间。 系统: 在内核模式下花费的时间

命令示例(ls-l所用的时间):

$ time ls -l

The above command runs "ls -l" and shows 
contents of current directory followed by
the time taken by command "ls -l".  

一个程序示例(fib(30)所用的时间): 让我们考虑下面的程序。

#include<stdio.h>
int fib( int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}
int main ()
{
printf ( "Fibonacci Number is %d" , fib(30));
return 0;
}


Let we save above program as fib.c.

// Compiling above program on shell
~$ gcc fib.c

// Running the generated executable with time
~$ time ./a.out
Fibonacci Number is 832040
real    0m0.017s
user    0m0.017s
sys    0m0.000s

Note: 0.017 seconds (shown with real) is total 
time taken by program.

这篇文章是有贡献的 德埃拉吉·古普塔 .如果你喜欢GeekSforgek,并且想贡献自己的力量,你也可以写一篇文章,并将文章邮寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享