其實可能係awk 比 perl, ruby 都快
在cygwin 是沒free 指令的, 所以用bash
寫一個簡陋的代用,後來又用其他的改寫一下,發覺
gawk 可能是最快速的
fang@bash ~
$ cat time_use
time bash -c '
step=$(( 1024 * 1024 ))
printf "\ttotal\tused\tfree\n"
while read name total used free
do
if [[ $name =~ (Mem:|Swap:) ]]
then
b=$(( $total / $step ))" M"
c=$(( $used / $step ))" M"
d=$(( $free / $step ))" M"
printf "$name\t$b\t$c\t$d\n"
fi
done < /proc/meminfo'
time perl -e '$step = 1024 * 1024 ;
print "\ttotal\tused\tfree\n" ;
open(FILENAME, "/proc/meminfo") ;
while (<FILENAME>) {
chomp ;
if (/Mem:|Swap:/){
@n = split(/\s+/);
$i = $n[0] ;
($a, $b, $c) = (int($n[1] / $step), int($n[2] / $step), int($n[3] / $step));
print "$i\t$a M\t$b M\t$c M\n" ;
}
}
close(FILENAME)'
time ruby -e 'class PrintHeader
def initialize(i="total", j="used", k="free")
@a = i
@b = j
@c = k
end
def header
print "\t", @a, "\t", @b, "\t", @c, "\n"
end
end
n = PrintHeader.new
n.header
class PrintContent
def initialize(fn="/proc/meminfo")
@filename = fn
end
def content
file = open(@filename)
while text = file.gets
if text =~ /Mem:|Swap:/
str = [ text.split(/\s+/) ]
step = 1024 * 1024
str.each { |i|
j = i[0]
a = i[1].to_i / step.to_i
b = i[2].to_i / step.to_i
c = i[3].to_i / step.to_i
print j, "\t", a," M", "\t", b," M", "\t", c," M", "\n"
}
end
end
file.close
end
end
zz = PrintContent.new
zz.content '
time gawk 'BEGIN {
print "\t", "total\t", "used\t", "free"
}
{
if ( $1 ~ /Mem:|Swap:/ ) {
step = 1024 * 1024
a = int( $2 / step )
b = int( $3 / step )
c = int( $4 / step )
print $1, "\t", a,"M\t", b,"M\t", c,"M"
}
}' /proc/meminfo
執行的畫面
fang@bash ~
$ sh time_use
total used free
Mem: 479 M 374 M 104 M
Swap: 480 M 72 M 407 M
real 0m0.109s
user 0m0.030s
sys 0m0.030s
total used free
Mem: 479 M 380 M 99 M
Swap: 480 M 72 M 407 M
real 0m0.344s
user 0m0.046s
sys 0m0.046s
total used free
Mem: 479 M 380 M 99 M
Swap: 480 M 72 M 407 M
real 0m0.203s
user 0m0.030s
sys 0m0.031s
total used free
Mem: 479 M 379 M 100 M
Swap: 480 M 72 M 407 M
real 0m0.047s
user 0m0.030s
sys 0m0.046s
fang@bash ~
$ sh time_use
total used free
Mem: 479 M 379 M 100 M
Swap: 480 M 72 M 407 M
real 0m0.062s
user 0m0.046s
sys 0m0.046s
total used free
Mem: 479 M 380 M 99 M
Swap: 480 M 72 M 407 M
real 0m0.078s
user 0m0.030s
sys 0m0.077s
total used free
Mem: 479 M 380 M 99 M
Swap: 480 M 72 M 407 M
real 0m0.063s
user 0m0.046s
sys 0m0.046s
total used free
Mem: 479 M 379 M 100 M
Swap: 480 M 72 M 407 M
real 0m0.047s
user 0m0.046s
sys 0m0.031s
fang@bash ~
$
可有其他網友測試過 ? :D