I tried using t1=$(date +%s%N)
to get the time in nanoseconds, but I kept in getting this error:
./script.sh: line 10: 1292460931N: value too g开发者_Go百科reat for base (error token is "1292460931N")
I looked up online and it seems that you can use the "time" command, however I can't find a good example of using the time command. Any help would be appreciated :)
The date
command you're using doesn't support %N
so your output is literally 1292460931N
. I tried it on Linux and it worked, but on FreeBSD I see the results you got. Run that date command in a shell and see what comes out. Is it possible you're using busybox
? Its cut-down date command also omits %N
but the version I just tried gave me 1292463535%N
.
Okay, a couple of things here.
First, not a lot of systems can give you a time that actually accurate to nanoseconds anyway.
Now, using time, either as /usr/bin/time
or the shell builtin (bash: help time
) is very easy. If the command you want to time is foo1
, then
$ time foo
will return the elapsed time as three lines on stderr
real 0m0.001s
user 0m0.000s
sys 0m0.000s
which you can use any way you like.
If you want to get a better, more accurate timing, execute the command many times. This can be as simple as writing a short loop
time for i in 0 1 2 3 4; do foo; done
will do foo
five times and give you the total time. You probably want to do more than 5 iterations, so you'd probably want a counter and a while loop or the like.
精彩评论