linux常用命令time怎么使用

linux常用命令time怎么使用,第1张

Linux中time命令,我们经常用来计算某个程序的运行耗时(real),用户态cpu耗时(user),系统态cpu耗时(sys)

time命令最常用的使用方式就是在其后面直接跟上命令和参数:

time <command>[<arguments...>]

举个栗子1:

bixiaopeng@bixiaopengtekiMacBook-Pro ~$ time sleep 2

real 0m2.005s

user0m0.001s

sys 0m0.002s

小解其意:

real 0m2.005s :sleep这个程序运行耗时为0m2.005s

user0m0.001s : 这个时间代表的是sleep运行在用户态的cpu时间

sys 0m0.002s : 这个时间代表的是sleep运行在核心态的cpu时间。

用户态(User Mode):

在用户态,代码不具备直接访问硬件或者访问内存的能力,而必须借助 *** 作系统提供的可靠的,底层的APIs来访问硬件或者内存。由于这种隔离带来的保护作用,用户态的代码崩溃(Crash),系统是可以恢复的。我们大多数的代码都是运行在用户态的。

核心态(Kernel Mode):

在内核态,代码拥有完全的,不受任何限制的访问底层硬件的能力。可以执行任意的CPU指令,访问任意的内存地址。内核态通常情况下,都是为那些最底层的,由 *** 作系统提供的,可信可靠的代码来运行的。内核态的代码崩溃将是灾难性的,它会影响到整个系统。

为什么要区分Kernel Mode 和 User Mode:

隔离保护,使得系统更稳定。

好,讲完用户态和核心态之后,我们来看user time,说过了,这个指的是程序foo运行在用户态的cpu时间,cpu时间不是墙上的钟走过的时间,而是指CPU工作时间。

举个栗子2:

#time -p 不显示单位

bixiaopeng@bixiaopengtekiMacBook-Pro ~$ time -p sleep 2

real 2.00

user 0.00

sys 0.00

可以使用腾讯手机管家监控的在手机上打开腾讯手机管家点击健康优化界面

在健康优化界面点击流量监控,然后在流量统计界面点击设

置流量套餐进入套餐设置向导在套餐设置向导界面,

输入套餐限额和月结算日然后点击下一步接着设置流量套餐。

后点击“完成腾讯手机管家的流量监控的基本设置已经完成。

1 sloccount源代码行数统计工具

--------------------------------------------------------------------------------

sloccount=Count Source Lines Of Code

官网 : http://www.dwheeler.com/sloccount/

1.1 Ubuntu安装

--------------------------------------------------------------------------------

sudo apt-get install sloccount

1.2 使用

--------------------------------------------------------------------------------

sloccount [--version] [--cached] [--append] [ --datadir directory ]

[--follow] [--duplicates] [--crossdups] [--autogen] [--multiproject]

[--filecount] [--wide] [--details] [ --effort F E ] [ --schedule F E ] [

--personcost cost ] [ --overhead overhead ] [ --addlang language ] [

--addlangall ] [--] directories

--cached

跳过计算过程,直接使用上次结果

参数

描述

–multiproject

如果该文件夹包括一系列的子文件夹,而它们中的每一个都是相对独立开发的不同的项目,那么使用”–multiproject”选项,评估将会正确的考虑到这一点

–filecount

显示文件数目而非代码行数

–details

显示每个源文件的详细信息

–duplicates

算上所有重复的(默认情况下如果文件有相同的内容,则只算一个)

–crossdups

如果顶目录包含几个不同的项目,并且你想把不同的项目下重复的文件在每个项目中都算上一次,则使用该选项

1.3 转换成html文件

--------------------------------------------------------------------------------

有一个sloc2html.py可以把生成的结果转换为带图形统计结果的html文件. 缺点是对中文支持不好

例如:

sloccount --wide --multiproject SourceDirectory >result.txt

sloc2html.py result.txt >result.html

再打开result.html即可看到结果形如:

下载地址 http://www.dwheeler.com/sloccount/sloc2html.py.txt

输出样例 http://www.dwheeler.com/sloccount/sloc2html-example.html

wget http://www.dwheeler.com/sloccount/sloc2html.py.txt -O sloc2html.py

sloc2html.py文件源代码如下

#!/usr/bin/env python

# Written by Rasmus Toftdahl Olesen [email protected]>

# Modified slightly by David A. Wheeler

# Released under the GNU General Public License v. 2 or higher

from string import *

import sys

NAME = "sloc2html"

VERSION = "0.0.2"

if len(sys.argv) != 2:

print "Usage:"

print "\t" + sys.argv[0] + " <sloc output file>"

print "\nThe output of sloccount should be with --wide and --multiproject formatting"

sys.exit()

colors = { "python" : "blue",

"ansic" : "yellow",

"perl" : "purple",

"cpp" : "green",

"sh" : "red",

"yacc" : "brown",

"lex" : "silver"

# Feel free to make more specific colors.

"ruby" : "maroon",

"cs" : "gray",

"java" : "navy",

"ada" : "olive",

"lisp" : "fuchsia",

"objc" : "purple",

"fortran" : "purple",

"cobol" : "purple",

"pascal" : "purple",

"asm" : "purple",

"csh" : "purple",

"tcl" : "purple",

"exp" : "purple",

"awk" : "purple",

"sed" : "purple",

"makefile" : "purple",

"sql" : "purple",

"php" : "purple",

"modula3" : "purple",

"ml" : "purple",

"haskell" : "purple"

}

print "<html>"

print "<head>"

print "<title>Counted Source Lines of Code (SLOC)</title>"

print "</head>"

print "<body>"

print "<h1>Counted Source Lines of Code</h1>"

file = open ( sys.argv[1], "r" )

print "<h2>Projects</h2>"

line = ""

while line != "SLOC\tDirectory\tSLOC-by-Language (Sorted)\n":

line = file.readline()

print "<table>"

print "<tr><th>Lines</th><th>Project</th><th>Language distribution</th></tr>"

line = file.readline()

while line != "\n":

num, project, langs = split ( line )

print "<tr><td>" + num + "</td><td>" + project + "</td><td>"

print "<table width=\"500\"><tr>"

for lang in split ( langs, "," ):

l, n = split ( lang, "=" )

print "<td bgcolor=\"" + colors[l] + "\" width=\"" + str( float(n) / float(num) * 500 ) + "\">" + l + "=" + n + " (" + str(int(float(n) / float(num) * 100)) + "%)</td>"

print "</tr></table>"

print "</td></tr>"

line = file.readline()

print "</table>"

print "<h2>Languages</h2>"

while line != "Totals grouped by language (dominant language first):\n":

line = file.readline()

print "<table>"

print "<tr><th>Language</th><th>Lines</th></tr>"

line = file.readline()

while line != "\n":

lang, lines, per = split ( line )

lang = lang[:-1]

print "<tr><td bgcolor=\"" + colors[lang] + "\">" + lang + "</td><td>" + lines + " " + per + "</td></tr>"

line = file.readline()

print "</table>"

print "<h2>Totals</h2>"

while line == "\n":

line = file.readline()

print "<table>"

print "<tr><td>Total Physical Lines of Code (SLOC):</td><td>" + strip(split(line,"=")[1]) + "</td></tr>"

line = file.readline()

print "<tr><td>Estimated development effort:</td><td>" + strip(split(line,"=")[1]) + " person-years (person-months)</td></tr>"

line = file.readline()

line = file.readline()

print "<tr><td>Schedule estimate:</td><td>" + strip(split(line,"=")[1]) + " years (months)</td></tr>"

line = file.readline()

line = file.readline()

print "<tr><td>Total estimated cost to develop:</td><td>" + strip(split(line,"=")[1]) + "</td></tr>"

print "</table>"

file.close()

print "Please credit this data as \"generated using 'SLOCCount' by David A. Wheeler.\"\n"

print "</body>"

print "</html>"


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7493423.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-06
下一篇 2023-04-06

发表评论

登录后才能评论

评论列表(0条)

保存