linux--Get Execution time

Source: Internet
Author: User
Tags add time time 0

Note: While performing some performance testing, sometimes we want to calculate the time a program runs, and sometimes it is possible to write a shell script to facilitate some performance test control (such as the wish to run an average of n times, etc.), in short, one of the requirements may be to get a timestamp or time difference.


1. Linux shell command to get time

The time command : Gets the execution time of a program, can obtain the actual running time and the program in the user state and the kernel state respectively, most of the performance test, may only need to pay attention to the actual time.

The use of the time command is simple, and it can be added before the original program to be run (executable, script, and so on).

Issue one: Common options for the time command

The usual options for using time are:-F and-P. Where-F to specify a format string, control the format of the output of time, the default time output is real, user, sys three line in XMXXX.XXXS format output, through-f control.

The-P option is also a format for using the POSIX standard format, and the main difference is that the displayed time is in S, and the specifics about the format refer to the Help content of man time.

PS:-F option not working? It is not clear why the-F and-O options are not working, know please add. (-P is working)

Another way to control the format is to set the TIMEFORMAT environment variable, specifically referring to man time to know what these format controls represent respectively. Examples are as follows:

    1. #time pwd
    2. /home/sgeng2
    3. Real 0m0.000s
    4. User 0m0.000s
    5. SYS 0m0.000s
    6. #export timeformat= "Real time%E, user time%u,sys time%s"
    7. #time pwd
    8. /home/sgeng2
    9. Real time 0.000, user time 0.000,sys time 0.000
    10. #time-P PWD
    11. /home/sgeng2
    12. Real 0.00
    13. User 0.00
    14. SYS 0.00
    15. #

PS: Very strange, feel that some of the format of time and the above options, as if not fully work, in short, the relationship is not big, the format is not important, the general use of-p in seconds as the unit is sufficient.


Issue two: Issue with the output of the time command

As mentioned above, as if the-o option does not work, then we can only find our own way. Sometimes writing a script, you want to output the results of the time to a file, and then may be based on the output of time to do some processing, such as the extraction of real times and so on. Obviously, what you can think of is redirection, as for the use of redirects here is obviously not ready to discuss (too complex), just a hint: the output of the time command is output to stderr, not stdout, so be careful when redirecting. Read the following example to understand the basic:

  1. #time pwd
  2. /home/sgeng2
  3. Real 0m0.000s
  4. User 0m0.000s
  5. SYS 0m0.000s
  6. #time pwd > OUT.txt
  7. Real 0m0.000s
  8. User 0m0.000s
  9. SYS 0m0.000s
  10. #cat OUT.txt
  11. /home/sgeng2
  12. #time pwd 2> OUT.txt
  13. /home/sgeng2
  14. Real 0m0.000s
  15. User 0m0.000s
  16. SYS 0m0.000s
  17. #cat OUT.txt
  18. # (Time pwd) 2> OUT.txt
  19. /home/sgeng2
  20. #cat OUT.txt
  21. Real 0m0.000s
  22. User 0m0.000s
  23. SYS 0m0.000s
  24. # (Time pwd) >& OUT.txt
  25. #cat OUT.txt
  26. /home/sgeng2
  27. Real 0m0.000s
  28. User 0m0.000s
  29. SYS 0m0.000s
  30. #

PS: Here more is involved and redirect related content, so no detailed analysis of each example. Note the difference between time pwd 2> OUT.txt and (time pwd) 2> OUT.txt, the previous meaning is to redirect the results of PWD stderr to OUT.txt, which is equivalent to "time (pwd 2> out.txt)" The result.


Date Command :

About the use of the date command, Baidu a large pile, it is not repeated, for example, you can refer to: http://xingfujie.blog.51cto.com/2791569/637223

Here are just a few common questions to ask:

Issue One: Date's%s and%n

There are many control formats in date, where%s is the time difference between getting the current 1970-01-01 00:00:00 UTC . Many other format control of date is to control the current time of the output format, such as only output time and minute, only the output date and so on, where%n is also this class,%n output is the current time of the nanosecond portion, because the date does not have a millisecond level of output, So the content below the second is the nanosecond part . So from this point of view, date can be very precise and can reach the nanosecond level.

Question two: Get a timestamp

Sometimes use timestamp, or random number, uuid such things, Baidu also has related articles (such as search "Shell date random number", etc.). In general, you can use the combination of%s and%n is no problem, in the same second, two runs%n certainly not the same, so the%s and%n combination can get a unique number.

    1. #date +%s.%n
    2. 1337435374.969263560
    3. #date +%s+%n
    4. 1337435377+310281496
    5. #date +%s_%n
    6. 1337435381_209334510
    7. #date +%s_%n
    8. 1337435383_169263078
    9. #date +%s_%n
    10. 1337435383_830009679
    11. #

PS: Sometimes you may want to use a "unique" thing to name a file, and so on, you can add time stamp.

2. The Linux shell gets the time difference (using the date command)

As for the use of the time command, it is itself getting a time difference, but obviously, time only applies to some simple cases, because the following arguments are what can be done, sometimes it may take more than one command to execute, and time is more troublesome.

(1) seconds

Date gets the "current time", obviously, two times run date can get a time difference, in theory, you can use many formats to represent the output of date, so as to calculate the time difference, but, obviously, the most direct way is to use the%s, so that you can directly calculate a difference, There is no need for such complex shell string processing. As follows:

    1. #start =$ (date +%s) && sleep 2 && end=$ (date +%s) && echo $ (($end-$start))
    2. 2
    3. #start =$ (date +%s) && sleep 3 && end=$ (date +%s) && echo $ (($end-$start))
    4. 3
    5. #


Of course, this is tested in terminal, so with && sequential execution of these commands, for the script, a line of writing is very good ....

    1. start=$ (Date +%s)
    2. ... what does for timing ...
    3. end=$ (Date +%s)
    4. time=$ (($end-$start))
    5. Echo $time

(2) ms Unit

More performance testing and other occasions to obtain the time difference, it is possible to hope accurate to Ms. In fact, using date can reach Ms. Go directly to the code.

  1. #! /bin/bash
  2. #filename: test.sh
  3. # Arg1=start, arg2=end, format:%s.%n
  4. function gettiming () {
  5. Start=$1
  6. End=$2
  7. start_s=$ (echo $start | cut-d '. '-F 1)
  8. start_ns=$ (echo $start | cut-d '. '-F 2)
  9. end_s=$ (echo $end | cut-d '. '-F 1)
  10. end_ns=$ (echo $end | cut-d '. '-F 2)
  11. # for Debug:
  12. # echo $start
  13. # echo $end
  14. Time=$ (((10# $end _s-10# $start _s) * + (10# $end _ns/1000000-10# $start _ns/1000000)))
  15. echo "$time MS"
  16. }
  17. echo "This was only a test to get a-MS Level time duration ..."
  18. start=$ (Date +%s.%n)
  19. LS >&/dev/null # Hey, be quite, does not output to console ....
  20. end=$ (Date +%s.%n)
  21. Gettiming $start $end

PS: This code is a simple test, you can get to the LS command execution time, I believe its execution time is short enough, if you need to get the difference in MS below, I believe you will not use the shell, hey, naturally rely on C to get it.

The results are as follows:

    1. #./test.sh
    2. This is the only a-test to get a-MS Level time duration ...
    3. 3 ms
    4. #./test.sh
    5. This is the only a-test to get a-MS Level time duration ...
    6. 2 ms
    7. #./test.sh
    8. This is the only a-test to get a-MS Level time duration ...
    9. 2 ms
    10. #

Still satisfied, can get to such a short time. Of course, in theory you can get to the time difference in NS, but, the individual think with the shell to get such a small time difference, you feel accurate ...

Description

The idea of the above code, in fact, is very simple,%s is the number of seconds from the standard time,%n is the current time of the next part of the second, then obviously, the%s and%n represents the current time of the full timestamp, the difference between the two timestamps is the time difference, the problem is how to deal with the problem, probably: first Use%s.% n format gets to the start and end time, and then takes the cut command from start and end to "." The part of the previous second and the part of the nanosecond after it (note: Here the insertion point between%s and%n, just as a separate function, any possible characters are possible, as long as the cut can be separated by the line); Then, subtract the seconds, get the difference of seconds, and convert the difference of millisecond. The difference is calculated by converting the part of the nanosecond to milliseconds (possibly negative); Finally, the sum of the two difference values is the true difference in milliseconds. It is easy to understand, the key is the use of cut, for the shell master, there should be many ways to extract the string, but for me such a non-Shell master, to myself with awk or sed what to write an extract of the regular, or very difficult, fortunately found the cut command, This kind of string can be easily extracted. So: Here's the method for reference only ...

About the "10#" in the code, this is to indicate that the next number is a 10-digit number, the reason for this is because the front of the nanosecond is 0 beginning, the shell as if 0 will default to be considered as eight, resulting in running an error, in short, Baidu a bit to find the reason, Here it simply adds all the numbers to the 10#, indicating that they are all 10 binary.

linux--Get Execution time

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.