Linux中是否有shell命令以毫秒为单位获取时间?
date +%s%N返回秒数+当前纳秒。
因此,echo $(($(date +%s%N)/1000000))就是您所需要的。
例子:
$ echo $(($(date +%s%N)/1000000))
1535546718115
Date +%s返回自epoch以来的秒数,如果有用的话。
日期+“% T。%N"返回当前时间,单位为纳秒。 06:46:41.431857000 日期+“% T。%6N”返回当前时间,其中纳秒四舍五入到前6位数字,即微秒。 06:47:07.183172 日期+“% T。%3N”返回当前时间,将纳秒四舍五入到前3位数字,即毫秒。 06:47:42.773
通常,date命令格式的每个字段都可以指定可选的字段宽度。
Nano是10−9,milli是10−3。因此,我们可以使用纳秒的前三个字符来得到毫秒:
date +%s%3N
男人约会:
%N纳秒(000000000..999999999) 1970-01-01 00:00:00 UTC以来的%s秒
来源:服务器故障如何在Bash中以毫秒为单位获得当前Unix时间?
在OS X上,date不支持%N标志,我建议使用Homebrew安装coreutils。这将使您能够访问一个名为gdate的命令,其行为与Linux系统上的date相同。
brew install coreutils
为了获得更“原生”的体验,你可以把这个添加到你的.bash_aliases中:
alias date='gdate'
然后执行
$ date +%s%N
在大多数情况下,其他答案可能已经足够了,但当我在BusyBox系统上遇到问题时,我想补充一下我的意见。
该系统不支持%N格式选项,也没有Python或Perl解释器。
在绞尽脑汁之后,我们(感谢戴夫!)想出了这个:
adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", $2) }'
它从adjtimex(通常用于设置系统时钟选项)的输出中提取秒和微秒,并打印它们而不添加新行(因此它们粘在一起)。请注意,microseconds字段必须预先用0填充,但这不会影响seconds字段,因为它长于6位数字。由此,将微秒转换为毫秒应该是很简单的。
如果你需要一个尾随的新行(也许因为它看起来更好),那么试试
adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", $2) }' && printf "\n"
还要注意,这需要adjtimex和awk可用。如果没有,那么与BusyBox你可以在本地指向他们:
ln -s /bin/busybox ./adjtimex
ln -s /bin/busybox ./awk
然后把上面的称为
./adjtimex | ./awk '/(time.tv_sec|time.tv_usec):/ { printf("%06d", $2) }'
当然,你也可以把它们放在你的PATH中
编辑:
以上这些都适用于我的BusyBox设备。在Ubuntu上我尝试了同样的事情,并意识到adjtimex有不同的版本。在Ubuntu上,这可以输出以秒为单位的时间,小数位为微秒(包括后面的新行)
sudo apt-get install adjtimex
adjtimex -p | awk '/raw time:/ { print $6 }'
不过我不会在Ubuntu上这么做。我会使用date +%s%N
date命令在OS X上没有提供毫秒,所以使用了python中的别名
millis(){ python -c "import time; print(int(time.time()*1000))"; }
OR
alias millis='python -c "import time; print(int(time.time()*1000))"'
编辑:跟随@CharlesDuffy的评论。 fork任何子进程都需要额外的时间。
$ time date +%s%N
1597103627N
date +%s%N 0.00s user 0.00s system 63% cpu 0.006 total
Python仍然在改进它的虚拟机启动时间,它没有提前编译的代码(比如日期)快。
在我的机器上,它花了大约30 - 60毫秒(即日期花费的6毫秒的5 -10倍)
$ time python -c "import time; print(int(time.time()*1000))"
1597103899460
python -c "import time; print(int(time.time()*1000))" 0.03s user 0.01s system 83% cpu 0.053 total
我认为awk比python轻量级,所以awk的范围是6ms到12ms(即日期的1x到2x):
$ time awk '@load "time"; BEGIN{print int(1000 * gettimeofday())}'
1597103729525
awk '@load "time"; BEGIN{print int(1000 * gettimeofday())}' 0.00s user 0.00s system 74% cpu 0.010 total
我只是想在Alper的回答中补充一下我必须做的事情:
在Mac上,你需要brew install coreutils,所以我们可以使用gdate。否则在Linux上,它只是日期。这个函数将帮助您执行命令,而无需创建临时文件或任何东西:
function timeit() {
start=`gdate +%s%N`
bash -c $1
end=`gdate +%s%N`
runtime=$(((end-start)/1000000000.0))
echo " seconds"
}
你可以将它与字符串一起使用:
timeit 'tsc --noEmit'
下面是一个在Linux上以毫秒为单位获取时间的便携式hack:
#!/bin/sh
read up rest </proc/uptime; t1="${up%.*}${up#*.}"
sleep 3 # your command
read up rest </proc/uptime; t2="${up%.*}${up#*.}"
millisec=$(( 10*(t2-t1) ))
echo $millisec
输出结果为:
3010
这是一个非常廉价的操作,它与shell内部程序和procfs一起工作。
Perl可以用于此目的,甚至在AIX这样的特殊平台上也是如此。例子:
#!/usr/bin/perl -w
use strict;
use Time::HiRes qw(gettimeofday);
my ($t_sec, $usec) = gettimeofday ();
my $msec= int ($usec/1000);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime ($t_sec);
printf "%04d-%02d-%02d %02d:%02d:%02d %03d\n",
1900+$year, 1+$mon, $mday, $hour, $min, $sec, $msec;
当你从4.1版本开始使用GNU AWK时,你可以加载时间库并执行以下操作:
$ awk '@load "time"; BEGIN{printf "%.6f", gettimeofday()}'
这将以秒为单位打印自1970-01-01T00:00:00以来的当前时间,精度为亚秒。
the_time = gettimeofday() Return the time in seconds that has elapsed since 1970-01-01 UTC as a floating-point value. If the time is unavailable on this platform, return -1 and set ERRNO. The returned time should have sub-second precision, but the actual precision may vary based on the platform. If the standard C gettimeofday() system call is available on this platform, then it simply returns the value. Otherwise, if on MS-Windows, it tries to use GetSystemTimeAsFileTime(). source: GNU awk manual
在Linux系统上,标准C函数getimeofday()以微秒精度返回时间。
纯bash解决方案
自bash 5.0(2019年1月7日发布)以来,您可以使用内置变量EPOCHREALTIME,它包含自epoch以来的秒数,包括小数点后精确到微秒(echo $EPOCHREALTIME输出类似1547624774.371210)。通过移除。最后三个地方是毫秒:
要么使用
(( t = ${EPOCHREALTIME/./} / 1000 ))
或者类似的东西
t=${EPOCHREALTIME/./} # remove the dot (s → µs)
t=${t%???} # remove the last three digits (µs → ms)
无论哪种方式t都是1547624774371。
输出十进制秒数:
start=$(($(date +%s%N)/1000000)) \
&& sleep 2 \
&& end=$(($(date +%s%N)/1000000)) \
&& runtime=$((end - start))
divisor=1000 \
&& foo=$(printf "%s.%s" $(( runtime / divisor )) $(( runtime % divisor ))) \
&& printf "runtime %s\n" $foo # in bash integer cannot cast to float
输出:runtime 2.3