Timing how long a python script runs

This post introduces several ways to find out how long a python script takes to complete its execution.

  • If you are using Linux or Mac OS, in your terminal
$ time ./your_script.py
  • Several ways to do the task by adding a few lines of code in your py script.
import time
startTime = time.time()

your_func() #python3: print ("It took", time.time() - startTime, "seconds.")

See the following for an example in python 3. 

import time
import functools

startTime = time.time()

print(functools.reduce(lambda x,y: x+y, [47,11,42,13]))

print ("It took", time.time() - startTime, "seconds.")

Another way to do the same thing:

from datetime import datetime
startTime = datetime.now()

#do something

#Python 2: 
print datetime.now() - startTime 

#Python 3: 
print(datetime.now() - startTime)

One more way to do the same thing with a nicely formatted output.

import sys
import timeit

startTime = timeit.default_timer()

#do some nice things...

stopTime = timeit.default_timer()
totalRunningTime = stopTime - startTime

# output running time in a nice format.
mins, secs = divmod(totalRunningTime, 60)
hours, mins = divmod(mins, 60)

sys.stdout.write("Total running time: %d:%d:%d.\n" % (hours, mins, secs))

If you want to compare two blocks of code / functions quickly you can do the following:

import timeit

startTime = timeit.default_timer()
print(timeit.default_timer() - startTime)

startTime2 = timeit.default_timer()
print(timeit.default_timer() - starTime2)

Dynamic GPU usage monitoring (CUDA)

To dynamically  monitor NVIDIA GPU usage, here I introduce two methods:

method 1: use nvidia-smi

in your terminal, issue the following command:

$ watch -n 1 nvidia-smi

It will continually update the gpu usage info (every second, you can change the 1 to 2 or the time interval you want the usage info to be updated).

method 2: use the open source monitoring program glances with its GPU monitoring plugin

in your terminal, issue the following command to install glances with its GPU monitoring plugin

$ sudo pip install glances[gpu]

to launch it, in your terminal, issue the following command:

 $ sudo glances

Then you should see your GPU usage etc. It also monitors the CPU, disk IO, disk space, network, and a few other things

For more commonly used Linux commands, check my other posts at here  and here .

Compile and Run C/C++ Programs on Linux

This post provides instructions how to compile and run c/C++ code on Linux.

Check whether gcc is installed

The following commands will display the installation path and version of gcc compiler.

$ whereis gcc
$ which gcc
$ gcc -v


Compile And Run C/C++ Programs In Linux

Write your code/program in your favorite text editor . Use extension .c for C programs or .cpp for C++ programs.

Here is a simple “C” program.

$ cat hellow.c
#include <stdio.h>
int main()
   printf("hello world!");
   return 0;

To compile the program, run:

$ gcc hello.c -o hello1


$ g++ hello.c -o hello1

In the above example, we used C++ compiler to compile the program. To use C compiler instead, run:

$ cc hello.c -o hello1

If there is any syntax or semantic errors in your code/program, they will be displayed on the screen. You need to fix them first to proceed further.

If there is no error then the compiler will successfully generate an executable file named hello1 in the current working directory (where you put the source c/c++ file).

Now you can execute the program using the following command:

$ ./hello1

To compile multiple source files (e.g., source1 and source2) into executable, run:

$ gcc sourcecode1.c sourcecode2.c -o executable

To allow warnings, debug symbols in the output:

$ gcc sourcecode.c -Wall -Og -o executable

To compile the source code into Assembler instructions:

$ gcc -S sourcecode.c

To compile the source code without linking:

$ gcc -c sourcecode.c

The above command will create a executable called sourcecode.o.

If your program contains math functions:

$ gcc sourcecode.c -o executable -lm

For more details, refer the man pages.

$ man gcc


Get the number of CPUs/cores in Linux from the command line

This post introduces how to find out the number of CPUs/cores on Linux machines from command line.

  • method 1:

$ nproc –all

20   # this means there are 20 cores on the linux machine.

  • method 2:

$ lscpu

CPU(s):                20

On-line CPU(s) list:   0-19

Thread(s) per core:    2

Core(s) per socket:    10