Memory profiling in Python using memory_profiler

To install memory_profiler:

    1. Profile function/script:
      Add following line in script to import memory profiler:

      Decorate the function you would like to profile with @profile
      Example:

      Run python script to get memory usage line by line.

      Another method to profile function is , decorate function with @profile and then run script using following command:

      2. Profile web2py application/(External scripts):
      Start webpy server using following command :

      Now open application from browser and open page you want to profile. Then stop web2py server. To view result run command ‘mprof plot’. But to plot result you need package ‘matplotlib’.
      To install matplotlib, first install required packages for matplotlib:

      Now install matplotlib:

      Now run following command to get memory profile graph (Memory Usage vs Time):

      The available commands for mprof :

      • mprof run: running an executable, recording memory usage
      • mprof plot: plotting one the recorded memory usage (by default, the last one)
      • mprof list: listing all recorded memory usage files.
      • mprof clean: delete recorded memory usage files.
      • mprof rm : delete particular  memory usage file.

Time profiling in python using cProfile

In this article we will cover following points:  Profile single function Profile complete python script How to read .prof file Profile web2py application To install cprofile on ubuntu use following … [Continue reading]

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 … [Continue reading]

Size Contest (SPOJ Challenge problem 378)

Given the set of integers, find the sum of all positive integers in it. Solutions can be sent in any language supported by SPOJ except Whitespace. Input t – number of test cases [t < 1000] On each of next t lines given a integer N [-1000 <= … [Continue reading]

Factorial (SPOJ problem 11)

View problem on SPOJ The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the … [Continue reading]

Check whether one string is rotated version of other(Josh software programming test problem-2)

Write a program to accept two strings str1 and str2 and checks whether str2 is rotated version of str1. Ex. Following are different values of str2 for given str1. GIVEN:str1="software" 2-character left rotate str2='resoft' 4-character left … [Continue reading]

Generate graph from input 1-D array (Josh Software programming test problem-1)

Josh software apti

Following program was asked by Josh Software during campus recruitment. Write a program to generate following chart without using any graph library.This is of marks from 1 to 5 obtained by 10 student represented by an array as … [Continue reading]

Remove duplicate characters in string

Write code to remove the duplicate characters in a string without using any additional buffer NOTE: One or two additional variables are fine An extra copy of the array is not. This question is from the book "cracking thecoding interview". Time … [Continue reading]

Recursive insertion sort

gaurav.vichare

[crayon-55e1e2ec5cdec302302359/]   … [Continue reading]

Snake game in C language using queue

Requirments: Turbo C++ compiler If you face any graphics error ,follow these steps. Go to "BGI" folder. ( C:\TurboC++\Disk\TurboC3\BGI). Copy file "EGAVGA.BGI"  Got "BIN" folder. ( C:\TurboC++\Disk\TurboC3\BIN). Paste "EGAVGA.BGI" file … [Continue reading]