Profiling Python code with the Unix time function

There are a large number of tools that one can use to profile the performance of code in Python. The simplest option for most is the Unix time command, which measures the amount of time or resources that executing a particular command requires. This recipe demonstrates the usage of this command.

Getting ready

Fortunately, the Unix time command is available on virtually every Linux distribution and on OS X at the command line. The time command is even available in Cygwin on Windows machines. Thus, you should be ready for this recipe.

How to do it…

Perform the following simple steps to conduct your first Python benchmark:

  1. Open a new terminal window.
  2. Change the directory to the location of asa.py.
  3. At the command prompt, type the following:
    time python asa.py 1R0R.pdb
    
  4. After about a minute—depending on your machine—the time command will report back two to three pieces of information—depending on your operating system—that describe the length of time consumed by executing your code:
    12078.1 angstrom squared.
    python asa.py 1R0R.pdb  49.60s user 0.15s system 97% cpu 51.194 total
    

    Note

    The asa.py script required 51.194 seconds to complete while running on a Mac Book Air with a 1.8 GHz Intel Core I5 processor, 8 GB 1600 MHz DDR3, and OSX 10.9.2. Also note that we are using the default setting of 960 sphere points in the script.

How it works…

There isn't much complexity to the time command, and it is a great tool to get a quick idea of how long a program or script takes to execute. As per the main page (enter man time at the command line to access), the time utility has output-formatting options that allow you to see various metrics that describe the memory and input/output usage. Also, the output is a little bit cryptic. In this case, 51.194 total is the elapsed actual time that the command took to complete with a 97% CPU utilization. The user and sys time values can be ignored.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.196.175