views:

277

answers:

2

Usually I use shell command time. My purpose is to test if data is small, medium, large or very large set, how much time and memory usage will be.

Any tools for linux or just python to do this?

+6  A: 

Have a look at timeit and the python profiler

They should give you an idea about where to look for bottlenecks.

Also, to get to grips with the output, have a look at this post

exhuma
Thanks! I'll try it
noomz
+1  A: 

Have a look at nose and at one of its plugins, this one in particular.

Once installed, nose is a script in your path, and that you can call in a directory which contains some python scripts:

$: nosetests

This will look in all the python files in the current directory and will execute any function that it recognizes as a test: for example, it recognizes any function with the word test_ in its name as a test.

So you can just create a python script called test_yourfunction.py and write something like this in it:

$: cat > test_yourfunction.py

def test_smallinput():
    yourfunction(smallinput)

def test_mediuminput():
    yourfunction(mediuminput)

def test_largeinput():
    yourfunction(largeinput)

Then you have to run

$: nosetest --with-profile --profile-stats-file yourstatsprofile.prof testyourfunction.py

and to read the profile file, use this python line:

python -c "import hotshot.stats ; stats = hotshot.stats.load('yourstatsprofile.prof') ; stats.sort_stats('time', 'calls') ; stats.print_stats(200)"
dalloliogm
Seems to me that this does the same as the profiler from the standard python library. Testing was not the topic of the question. Plus: `nose` relies on hotshot. It's no longer maintained since Python 2.5 and is only kept "for specialized usage"
exhuma