{"id":197886,"date":"2020-08-12T08:38:57","date_gmt":"2020-08-12T00:38:57","guid":{"rendered":"https:\/\/gulass.cn\/?p=197886"},"modified":"2020-08-06T09:39:55","modified_gmt":"2020-08-06T01:39:55","slug":"gnu-time-linux","status":"publish","type":"post","link":"https:\/\/gulass.cn\/gnu-time-linux.html","title":{"rendered":"\u5982\u4f55\u7528GNU time\u67e5\u627eLinux\u547d\u4ee4\u6216\u8fdb\u7a0b\u7684\u6267\u884c\u65f6\u95f4"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u7c7b Unix \u7cfb\u7edf\u4e2d\uff0c\u4f60\u53ef\u80fd\u77e5\u9053\u4e00\u4e2a\u547d\u4ee4\u6216\u8fdb\u7a0b\u5f00\u59cb\u6267\u884c\u7684\u65f6\u95f4\uff0c\u4ee5\u53ca\u4e00\u4e2a\u8fdb\u7a0b\u8fd0\u884c\u4e86\u591a\u4e45\u3002 \u4f46\u662f\uff0c\u4f60\u5982\u4f55\u77e5\u9053\u8fd9\u4e2a\u547d\u4ee4\u6216\u8fdb\u7a0b\u4f55\u65f6\u7ed3\u675f\u6216\u8005\u5b83\u5b8c\u6210\u8fd0\u884c\u6240\u82b1\u8d39\u7684\u603b\u65f6\u957f\u5462\uff1f<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5728\u7c7b Unix \u7cfb\u7edf\u4e2d\uff0c\u8fd9\u662f\u975e\u5e38\u5bb9\u6613\u7684! \u6709\u4e00\u4e2a\u4e13\u95e8\u4e3a\u6b64\u8bbe\u8ba1\u7684\u7a0b\u5e8f\u540d\u53eb GNU time\u3002 \u4f7f\u7528 time \u7a0b\u5e8f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u6d4b\u91cf Linux \u64cd\u4f5c\u7cfb\u7edf\u4e2d\u547d\u4ee4\u6216\u7a0b\u5e8f\u7684\u603b\u6267\u884c\u65f6\u95f4\u3002 time \u547d\u4ee4\u5728\u5927\u591a\u6570 Linux \u53d1\u884c\u7248\u4e2d\u90fd\u6709\u9884\u88c5\uff0c\u6240\u4ee5\u4f60\u4e0d\u5fc5\u53bb\u5b89\u88c5\u5b83\u3002<\/p>\n

\"\"<\/p>\n

\u5728 Linux \u4e2d\u67e5\u627e\u4e00\u4e2a\u547d\u4ee4\u6216\u8fdb\u7a0b\u7684\u6267\u884c\u65f6\u95f4<\/strong><\/div>\n

\u8981\u6d4b\u91cf\u4e00\u4e2a\u547d\u4ee4\u6216\u7a0b\u5e8f\u7684\u6267\u884c\u65f6\u95f4\uff0c\u8fd0\u884c\uff1a<\/p>\n

$ \/usr\/bin\/time -p ls\r\n\r\n\u6216\u8005\uff0c<\/pre>\n
$ time ls\r\n\u8f93\u51fa\u6837\u4f8b\uff1a<\/pre>\n
dir1 dir2 file1 file2 mcelog\r\nreal 0m0.007s\r\nuser 0m0.001s\r\nsys 0m0.004s\r\n$ time ls -a\r\n. .bash_logout dir1 file2 mcelog .sudo_as_admin_successful\r\n.. .bashrc dir2 .gnupg .profile .wget-hsts\r\n.bash_history .cache file1 .local .stack\r\nreal 0m0.008s\r\nuser 0m0.001s\r\nsys 0m0.005s<\/pre>\n

\u4ee5\u4e0a\u547d\u4ee4\u663e\u793a\u51fa\u4e86 ls \u547d\u4ee4\u7684\u603b\u6267\u884c\u65f6\u95f4\u3002 \u4f60\u53ef\u4ee5\u5c06 ls \u66ff\u6362\u4e3a\u4efb\u4f55\u547d\u4ee4\u6216\u8fdb\u7a0b\uff0c\u4ee5\u67e5\u627e\u603b\u7684\u6267\u884c\u65f6\u95f4\u3002<\/p>\n

\u8f93\u51fa\u8be6\u89e3\uff1a<\/p>\n

real \u2014\u2014 \u6307\u7684\u662f\u547d\u4ee4\u6216\u7a0b\u5e8f\u6240\u82b1\u8d39\u7684\u603b\u65f6\u95f4
\nuser \u2014\u2014 \u6307\u7684\u662f\u5728\u7528\u6237\u6a21\u5f0f\u4e0b\u7a0b\u5e8f\u6240\u82b1\u8d39\u7684\u65f6\u95f4
\nsys \u2014\u2014 \u6307\u7684\u662f\u5728\u5185\u6838\u6a21\u5f0f\u4e0b\u7a0b\u5e8f\u6240\u82b1\u8d39\u7684\u65f6\u95f4<\/p>\n

time \u4e0e \/usr\/bin\/time<\/strong><\/div>\n

\u4f60\u53ef\u80fd\u6ce8\u610f\u5230\u4e86, \u6211\u4eec\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86\u4e24\u4e2a\u547d\u4ee4 time \u548c \/usr\/bin\/time \u3002 \u6240\u4ee5\uff0c\u4f60\u53ef\u80fd\u4f1a\u60f3\u77e5\u9053\u4ed6\u4eec\u7684\u4e0d\u540c\u3002<\/p>\n

\u9996\u5148\uff0c \u8ba9\u6211\u4eec\u4f7f\u7528 type \u547d\u4ee4\u770b\u770b time \u547d\u4ee4\u5230\u5e95\u662f\u4ec0\u4e48\u3002\u5bf9\u4e8e\u90a3\u4e9b\u6211\u4eec\u4e0d\u4e86\u89e3\u7684 Linux \u547d\u4ee4\uff0ctype \u547d\u4ee4\u7528\u4e8e\u67e5\u627e\u76f8\u5173\u547d\u4ee4\u7684\u4fe1\u606f\u3002<\/p>\n

$ type -a time\r\ntime is a shell keyword\r\ntime is \/usr\/bin\/time<\/pre>\n

\u6b63\u5982\u4f60\u5728\u4e0a\u9762\u7684\u8f93\u51fa\u4e2d\u770b\u5230\u7684\u4e00\u6837\uff0ctime \u662f\u4e24\u4e2a\u4e1c\u897f\uff1a<\/p>\n

\u4e00\u4e2a\u662f BASH shell \u4e2d\u5185\u5efa\u7684\u5173\u952e\u5b57
\n\u4e00\u4e2a\u662f\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5982 \/usr\/bin\/time\u3002<\/p>\n

\u7531\u4e8e shell \u5173\u952e\u5b57\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5f53\u4f60\u6ca1\u6709\u7ed9\u51fa\u5b8c\u6574\u8def\u5f84\u53ea\u8fd0\u884c time \u547d\u4ee4\u65f6\uff0c\u4f60\u8fd0\u884c\u7684\u662f shell \u5185\u5efa\u7684\u547d\u4ee4\u3002 \u4f46\u662f\uff0c\u5f53\u4f60\u8fd0\u884c \/usr\/bin\/time \u65f6\uff0c\u4f60\u8fd0\u884c\u7684\u662f\u771f\u6b63\u7684 GNU time \u547d\u4ee4\u3002 \u56e0\u6b64\uff0c\u4e3a\u4e86\u6267\u884c\u771f\u6b63\u7684\u547d\u4ee4\u4f60\u53ef\u80fd\u9700\u8981\u7ed9\u51fa\u5b8c\u6574\u8def\u5f84\u3002<\/p>\n

\u5728\u5927\u591a\u6570 shell \u4e2d\u5982 BASH\u3001ZSH\u3001CSH\u3001KSH\u3001TCSH \u7b49\uff0c\u5185\u5efa\u7684\u5173\u952e\u5b57 time \u662f\u53ef\u7528\u7684\u3002 time \u5173\u952e\u5b57\u7684\u9009\u9879\u5c11\u4e8e\u8be5\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u7684\u552f\u4e00\u9009\u9879\u662f -p\u3002<\/p>\n

\u4f60\u73b0\u5728\u77e5\u9053\u4e86\u5982\u4f55\u4f7f\u7528 time \u547d\u4ee4\u67e5\u627e\u7ed9\u5b9a\u547d\u4ee4\u6216\u8fdb\u7a0b\u7684\u603b\u6267\u884c\u65f6\u95f4\u3002 \u60f3\u8fdb\u4e00\u6b65\u4e86\u89e3 GNU time \u5de5\u5177\u5417\uff1f \u7ee7\u7eed\u9605\u8bfb\u5427\uff01<\/p>\n

\u5173\u4e8e GNU time \u7a0b\u5e8f\u7684\u7b80\u8981\u4ecb\u7ecd<\/strong><\/div>\n

GNU time \u7a0b\u5e8f\u8fd0\u884c\u5e26\u6709\u7ed9\u5b9a\u53c2\u6570\u7684\u547d\u4ee4\u6216\u7a0b\u5e8f\uff0c\u5e76\u5728\u547d\u4ee4\u5b8c\u6210\u540e\u5c06\u7cfb\u7edf\u8d44\u6e90\u4f7f\u7528\u60c5\u51b5\u6c47\u603b\u5230\u6807\u51c6\u8f93\u51fa\u3002 \u4e0e time \u5173\u952e\u5b57\u4e0d\u540c\uff0cGNU time \u7a0b\u5e8f\u4e0d\u4ec5\u663e\u793a\u547d\u4ee4\u6216\u8fdb\u7a0b\u7684\u6267\u884c\u65f6\u95f4\uff0c\u8fd8\u663e\u793a\u5185\u5b58\u3001I\/O \u548c IPC \u8c03\u7528\u7b49\u5176\u4ed6\u8d44\u6e90\u3002<\/p>\n

time \u547d\u4ee4\u7684\u8bed\u6cd5\u662f\uff1a<\/p>\n

\/usr\/bin\/time [options] command [arguments...]<\/pre>\n

\u4e0a\u8ff0\u8bed\u6cd5\u4e2d\u7684 options \u662f\u6307\u4e00\u7ec4\u53ef\u4ee5\u4e0e time \u547d\u4ee4\u4e00\u8d77\u4f7f\u7528\u53bb\u6267\u884c\u7279\u5b9a\u529f\u80fd\u7684\u9009\u9879\u3002 \u4e0b\u9762\u7ed9\u51fa\u4e86\u53ef\u7528\u7684\u9009\u9879\uff1a<\/p>\n

    \n
  • -f, \u2013format\u2014\u2014 \u4f7f\u7528\u6b64\u9009\u9879\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u6307\u5b9a\u8f93\u51fa\u683c\u5f0f\u3002<\/li>\n
  • -p, \u2013portability\u2014\u2014 \u4f7f\u7528\u7b80\u8981\u7684\u8f93\u51fa\u683c\u5f0f\u3002<\/li>\n
  • -o file, \u2013output=FILE\u2014\u2014 \u5c06\u8f93\u51fa\u5199\u5230\u6307\u5b9a\u6587\u4ef6\u4e2d\u800c\u4e0d\u662f\u5230\u6807\u51c6\u8f93\u51fa\u3002<\/li>\n
  • -a, \u2013append\u2014\u2014 \u5c06\u8f93\u51fa\u8ffd\u52a0\u5230\u6587\u4ef6\u4e2d\u800c\u4e0d\u662f\u8986\u76d6\u5b83\u3002<\/li>\n
  • -v, \u2013verbose \u2014\u2014 \u6b64\u9009\u9879\u663e\u793a time\u547d\u4ee4\u8f93\u51fa\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/li>\n
  • \u2013quiet \u2013 \u6b64\u9009\u9879\u53ef\u4ee5\u9632\u6b62 time\u547d\u4ee4\u62a5\u544a\u7a0b\u5e8f\u7684\u72b6\u6001.<\/li>\n<\/ul>\n

    \u5f53\u4e0d\u5e26\u4efb\u4f55\u9009\u9879\u4f7f\u7528 GNU time \u547d\u4ee4\u65f6\uff0c\u4f60\u5c06\u770b\u5230\u4ee5\u4e0b\u8f93\u51fa\u3002<\/p>\n

    $ \/usr\/bin\/time wc \/etc\/hosts\r\n9 28 273 \/etc\/hosts\r\n0.00user 0.00system 0:00.00elapsed 66%CPU (0avgtext+0avgdata 2024maxresident)k\r\n0inputs+0outputs (0major+73minor)pagefaults 0swaps<\/pre>\n

    \u5982\u679c\u4f60\u7528 shell \u5173\u952e\u5b57 time \u8fd0\u884c\u76f8\u540c\u7684\u547d\u4ee4, \u8f93\u51fa\u4f1a\u6709\u4e00\u70b9\u513f\u4e0d\u540c\uff1a<\/p>\n

    $ time wc \/etc\/hosts\r\n9 28 273 \/etc\/hosts\r\nreal 0m0.006s\r\nuser 0m0.001s\r\nsys 0m0.004s<\/pre>\n

    \u6709\u65f6\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u5c06\u7cfb\u7edf\u8d44\u6e90\u4f7f\u7528\u60c5\u51b5\u8f93\u51fa\u5230\u6587\u4ef6\u4e2d\u800c\u4e0d\u662f\u7ec8\u7aef\u4e0a\u3002 \u4e3a\u6b64\uff0c \u4f60\u53ef\u4ee5\u4f7f\u7528 -o \u9009\u9879\uff0c\u5982\u4e0b\u6240\u793a\u3002<\/p>\n

    $ \/usr\/bin\/time -o file.txt ls\r\ndir1 dir2 file1 file2 file.txt mcelog<\/pre>\n

    \u6b63\u5982\u4f60\u770b\u5230\u7684\uff0ctime \u547d\u4ee4\u4e0d\u4f1a\u663e\u793a\u5230\u7ec8\u7aef\u4e0a\u3002\u56e0\u4e3a\u6211\u4eec\u5c06\u8f93\u51fa\u5199\u5230\u4e86file.txt \u7684\u6587\u4ef6\u4e2d\u3002 \u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u8fd9\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff1a<\/p>\n

    $ cat file.txt\r\n0.00user 0.00system 0:00.00elapsed 66%CPU (0avgtext+0avgdata 2512maxresident)k\r\n0inputs+0outputs (0major+106minor)pagefaults 0swaps<\/pre>\n

    \u5f53\u4f60\u4f7f\u7528 -o \u9009\u9879\u65f6, \u5982\u679c\u4f60\u6ca1\u6709\u4e00\u4e2a\u540d\u4e3a file.txt \u7684\u6587\u4ef6\uff0c\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2a\u5e76\u628a\u8f93\u51fa\u5199\u8fdb\u53bb\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u5b83\u4f1a\u8986\u76d6\u6587\u4ef6\u539f\u6765\u7684\u5185\u5bb9\u3002<\/p>\n

    \u4f60\u53ef\u4ee5\u4f7f\u7528 -a \u9009\u9879\u5c06\u8f93\u51fa\u8ffd\u52a0\u5230\u6587\u4ef6\u540e\u9762\uff0c\u800c\u4e0d\u662f\u8986\u76d6\u5b83\u7684\u5185\u5bb9\u3002<\/p>\n

    $ \/usr\/bin\/time -a file.txt ls<\/pre>\n

    -f \u9009\u9879\u5141\u8bb8\u7528\u6237\u6839\u636e\u81ea\u5df1\u7684\u559c\u597d\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002 \u6bd4\u5982\u8bf4\uff0c\u4ee5\u4e0b\u547d\u4ee4\u7684\u8f93\u51fa\u4ec5\u663e\u793a\u7528\u6237\uff0c\u7cfb\u7edf\u548c\u603b\u65f6\u95f4\u3002<\/p>\n

    $ \/usr\/bin\/time -f \"\\t%E real,\\t%U user,\\t%S sys\" ls\r\ndir1 dir2 file1 file2 mcelog\r\n0:00.00 real, 0.00 user, 0.00 sys<\/pre>\n

    \u8bf7\u6ce8\u610f shell \u4e2d\u5185\u5efa\u7684 time \u547d\u4ee4\u5e76\u4e0d\u5177\u6709 GNU time \u7a0b\u5e8f\u7684\u6240\u6709\u529f\u80fd\u3002<\/p>\n

    \u6709\u5173 GNU time \u7a0b\u5e8f\u7684\u8be6\u7ec6\u8bf4\u660e\u53ef\u4ee5\u4f7f\u7528 man \u547d\u4ee4\u6765\u67e5\u770b\u3002<\/p>\n

    $ man time<\/pre>\n

    \u60f3\u8981\u4e86\u89e3\u6709\u5173 Bash \u5185\u5efa time \u5173\u952e\u5b57\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u8fd0\u884c\uff1a<\/p>\n

    $ help time<\/pre>\n

    \u5c31\u5230\u8fd9\u91cc\u5427\u3002 \u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u5728\u7c7b Unix \u7cfb\u7edf\u4e2d\uff0c\u8fd9\u662f\u975e\u5e38\u5bb9\u6613\u7684! \u6709\u4e00\u4e2a\u4e13\u95e8\u4e3a\u6b64\u8bbe\u8ba1\u7684\u7a0b\u5e8f\u540d\u53eb GNU time\u3002 \u4f7f\u7528 time \u7a0b […]<\/p>\n","protected":false},"author":323,"featured_media":34213,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-197886","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/197886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/323"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=197886"}],"version-history":[{"count":2,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/197886\/revisions"}],"predecessor-version":[{"id":197894,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/197886\/revisions\/197894"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/34213"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=197886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=197886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=197886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}