Linux commands to detect the computer resource usage of a program
假设有两个程序
以编程方式,我会使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <sys/time.h> #include <sys/resource.h> #include <unistd.h> int main (int argc, char *argv[]) { struct rusage start; struct rusage end; getrusage (RUSAGE_SELF, &start); // get time at start some_function (); // Function to measure getrusage (RUSAGE_SELF, &end); // get time at end printf ("System: %d usecs, User: %d usecs ", end.ru_stime.tv_usec - start.ru_stime.tv_usec, end.ru_utime.tv_usec - start.ru_utime.tv_usec); ... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | struct rusage { struct timeval ru_utime; // user time used struct timeval ru_stime; // system time used long ru_maxrss; // maximum resident set size long ru_ixrss; // integral shared memory size long ru_idrss; // integral unshared data size long ru_isrss; // integral unshared stack size long ru_minflt; // page reclaims long ru_majflt; // page faults long ru_nswap; // swaps long ru_inblock; // block input operations long ru_oublock; // block output operations long ru_msgsnd; // messages sent long ru_msgrcv; // messages received long ru_nsignals; // signals received long ru_nvcsw; // voluntary context switches long ru_nivcsw; // involuntary context switches }; |
要获得该计划的时间,您可以点击以下链接。 它显示了如何使用
在shell中获取程序执行时间
有关内存资源,请查看以下链接,了解如何在linux中使用
http://linux.about.com/od/commands/l/blcmdl1_top.htm
尝试
1 2 3 4 | /usr/bin/time --format="real\t%e user\t%U sys\t%S mem:\t%M" -- ./a.out |
使用时间可以为您提供程序的真实时间,用户时间和系统时间。
例如
1 | time ./a.out |
top命令可用于内存使用。