A quick guide to profiling programs on a GNU/Linux system.

Required packages:

1. gcc - The gnu compiler collection. This is installed by default on every GNU/Linux system. You may need to install the libc header files, which some distributins skip by default. On a ubuntu system, you can install the libc header files using the command
     $ sudo apt-get install libc-dev
2. gnuplot - This program is used to produce a cartesian graph of the input_size vs run_time for the program. On an ubuntu system you can install by the command
     $ sudo apt-get install gnuplot

Sample Program:


     This is a program to sort a list of randomly generated numbers using the bubble sort algorithm.

Timing:

     We use the system call gettimeofday() to get the current time upto a microsecond accuracy. Just before the algorithm starts and at the end, we note the current times, and subtract the values to get the run time of the program. The usage of gettimeofday() and manipulation of the time values is illustrated in the sample program.

     The end result required is the running time of the program for a given input size. Run the program for different values of the input size and note down the running time each time. 

Preparing the data:

     Gnuplot requires the input data to be in the following format.

xval1 yval1
xval2 yval2
xval3 yval3
.......
      Each xval-yval pair being seperated by a newline and a space between the related values.
      Enter the values you noted down into a text file (using an editor of your choice) in the above described format. The entire process may be simplified if the program is modified to accept the input-length via a command line argument, and automatically prints the inputsize-runtime pair for each run. You can then redirect this output to a file.
      Here is a sample set of data:        
            1000 7.671000
            2000 44.349000
            3000 91.610000
            4000 145.347000
            5000 216.070000
            6000 294.730000
            7000 394.678000
            8000 506.538000
            9000 642.290000
            10000 789.100000
            11000 981.574000
            12000 1125.924000
     The left column gives the size of the input and the right column gives the runtime in milliseconds.
     Lets call the data file as graph.data.

Gnuplot:

     Once the above data file is ready,
enter gnuplot by typing "gnuplot" on your shell (command-line). You must now have a gnuplot prompt like this. 
             gnuplot>

      If you are on a standalone GNU/Linux system, i.e, GNU/Linux is installed on the system that you're working and you are running X (GUI), then the following command will plot the graph.
             gnuplot> plot "graph.data"
The filename containing the data is in quotes.

      In case you are on a 'terminal-only' login, i.e, logged in to a remote system via ssh, then the above method will not work. You need to change the terminal type in gnuplot. to do this, run the following command.
              gnuplot>set terminal dumb

      This will set the terminal in gnuplot such that the graph is plottable on any terminal that can display ASCII characters. You can then give the command for plotting the data. 
              gnuplot> plot "graph.data"

Gnuplot can also output the graph in jpeg/gif/png images. You can do this as follows.
              gnuplot>set terminal jpeg
              gnuplot>set output "graph.jpg"
              gnuplot>plot "graph.data"

You must now have a runtime profile of your program.

Any suggestions and feedbacks is appreciated.
Contact: vaivaswatha[at]yahoo[dot]co[dot]in
©Copyright <2009> <Vaivaswatha N>