Tag Archives: python

Use Python to time c++ binaries from the command line

Often I want to benchmark c++ code to try to understand if I’ve made things faster or slower. Though I don’t doubt there are possibly better and possibly more accurate measures out there, I have yet to find a straightforward and clear answer to this question. But this may be trivially easy in Python, using timeit.

This assumes you have Python installed (of some compatible version, this is only tested on 2.7). Assuming your c++ binary name is called “a.out” and is located in the directory you’re currently in, you can run:

$ python -m timeit -n 1 'import subprocess; subprocess.call("./a.out", shell=True)'

What I know:
0. Respect the quotes.
1. The option “-n 1” tells you the number of times to run the function. n=1 appears to run the function 3 times and report the best time of those 3.
2. The python function we’re using here is in the module subprocess and is called “call”.

A few real notes:
0. Note that this function will not purely benchmark your code, as there is technically some overhead with import subprocess and running subprocess.call. If you care about this, then you’ll need another solution. You might be able to get a sense of the overhead on your machine by running:

$ python -m timeit -n 1 'import subprocess; subprocess.call("", shell=True)'

This runs in 2.5 ms on my laptop. You could assume linearity here and just subtract off this time, if you don’t mind the back-of-the-envelopeness of this.

1. There might be a hundred caveats on this, with respect to subprocess and shell=True, which are details I have yet to fully figure out. But again this is a qnd benchmark, good enough for my uses, and possibly useful to others.

Installing NEURON, MPI, and Python on OS X Lion

I’ve recently run into the problem of trying to compile the source for NEURON 7.3a that includes support for parallel NEURON (using MPI) and Python on Mac OS X Lion. Using a number of helpful web resources, I wanted to cobble together an “as-of-this-writing” practice to get a kitchen sink working install for all components.

UPDATED 01-Sept-2012: Added instructions for mpi4py

Because I don’t have the resources to test many version combinations, etc., this assumes OS X 10.7.4 on a Retina MacBook Pro 10,1 (Mid 2012), with a working copy of XCode 4.4.1 installed (available for free from the Mac App Store). I suspect but cannot verify that this will work with many different versions of all of these components. Note: MacPorts is particularly sensitive to very new XCode and OS releases, so right after a new one, things don’t always work right away.

Finally this assumes you have admin access to the computer on which you are installing things and that you will use sudo for good and not evil.

And super-finally, this is basically an aggregate of web sources, some of which I had to modify to get it working. Sources are inline, below. Much is duplicated here because of the transience of web links.

Continue reading

Python Startup on Mac OS X Leopard

The promise of the open source programming language Python for numerical computing has been frustrating for lack of good, consolidated advice out there to troubleshoot issues that should be really simple. Here’s a brief tutorial on how to get readline support on Mac OS X 10.5.x.

What is readline? Readline is a program that allows you to use tab completion, history, and other features that you’re used to in your favorite shell, MATLAB from the command line, and GNU Octave.

There are two steps to this. The first is to tell python to always use a startup file. The second is the create the startup file with the right info inside. I’m assuming you use Bash and are running python from a terminal — in xterm, Terminal.app, or iTerm.app, etc.

Step 1: In your .bashrc file (or .profile, .bash_profile, etc.), using your favorite text editor, put the following line:

export PYTHONSTARTUP=~/.pythonrc

To initialize this, at your shell, type:

source ~/.bashrc

(substitute your file appropriately)

Step 2: Create the file .pythonrc in your home directory (~/):

import rlcompleter
import readline
readline.parse_and_bind ("tab: complete")

Save this file, and now run any python build you may have on your machine. Python will read this file automatically and now you can test out tab completion by typing (no quotes): ‘imp’. The word “import” should be completed.