Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to measure performance of code ? – Basic methods

In this article, we will cover some very basic methods to calculate performance of script. In next article we will cover advance tool like cProfile and memory_profiler.

What is profiling?

  • Profiling is program analysis that measures, for example, the space (memory) or time complexity of a program, the usage of particular instructions, or the frequency and duration of function calls.
  • Profiling or performance measurement is main tool for code optimization.
  • There are two types of profiling : Time profiling and Memory profiling
  • We should also consider memory leaks to optimize code.

1) Date

Run script by running date command before and after script to find time when script started execution and when it completed.

date; python; date

This prints time before running script and again print time when execution is complete. So we can find execution time by subtracting end time from start time.

2) time

Unix time command is helpful to figure out how much time command, shell script or python program takes for execution.

time python


python  0.02s user 0.02s system 15% cpu 0.301 total

  • User : CPU time spent in user-mode (outside the kernel)
  • System : CPU time spent in the kernel
  • CPU : Percent of CPU this job got
  • Total : elapsed time, start to end

Why user + system ≠ total ?
total represents actual elapsed time, while user and sys values represent CPU execution time.

3) /usr/bin/time

/usr/bin/time gives more detailed output than ‘time‘.

/usr/bin/time -v


/usr/bin/time: cannot run No such file or directory
Command exited with non-zero status 127
	Command being timed: ""
	User time (seconds): 0.00
	System time (seconds): 0.00
	Percent of CPU this job got: ?%
	Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
	Average shared text size (kbytes): 0
	Average unshared data size (kbytes): 0
	Average stack size (kbytes): 0
	Average total size (kbytes): 0
	Maximum resident set size (kbytes): 344
	Average resident set size (kbytes): 0
	Major (requiring I/O) page faults: 0
	Minor (reclaiming a frame) page faults: 72
	Voluntary context switches: 1
	Involuntary context switches: 0
	Swaps: 0
	File system inputs: 0
	File system outputs: 0
	Socket messages sent: 0
	Socket messages received: 0
	Signals delivered: 0
	Page size (bytes): 4096
	Exit status: 127

What is difference between usr/bin/time and time ?
time is built in unix command and usr/bin/time is GNU time

The post How to measure performance of code ? – Basic methods appeared first on Gaurav Vichare.

This post first appeared on Gaurav Vichare -, please read the originial post: here

Share the post

How to measure performance of code ? – Basic methods


Subscribe to Gaurav Vichare -

Get updates delivered right to your inbox!

Thank you for your subscription