Bash’s builtin command printf is always a fascinating one to me and it amazed me when found out it supports time format for Unix time. A quick test shows the performance comparing to using external command date:
time for ((i=0; i<1000; i++)); do date -d @0 +%c >/dev/null; done time for ((i=0; i<1000; i++)); do printf "%(%c)T\n" 0 >/dev/null; done
real 0m2.108s user 0m0.215s sys 0m1.117s real 0m0.045s user 0m0.032s sys 0m0.012s
More than 50 times faster if the code is to print out a formatted time with Unix time.
If you need the current time, you still don’t need to use date, printf can do that very much, with -1 as the time:
printf '%(%c)T\n' -1 printf -v now '%(%s)T' -1 echo $now # equals to now=$(date +%s) echo $now
Fri 10 May 2013 02:03:59 PM CST 1368165839 1368165839
-v option is another great thing about printf.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.