views:

451

answers:

5

I have a memory and CPU intensive problem to solve and I need to benchmark the different solutions in ruby and python on different platforms.

To do the benchmark, I need to measure the time taken and the memory occupied by objects (not the entire program, but a selected list of objects) in both python and ruby.

Please recommend ways to do it, and also let me know if it is possible to do it without using OS specify tools like (Task Manager and ps). Thanks!

Update: Yes, I know that both Python and Ruby are not strong in performance and there are better alternatives like c, c++, Java etc. I am actually more interested in comparing the performance of Python and Ruby. And please no fame-wars.

+3  A: 

For Python I recommend heapy

from guppy import hpy
h = hpy()
print h.heap()

or Dowser or PySizer

For Ruby you can use the BleakHouse Plugin or just read this answer on memory leak debugging (ruby).

tuergeist
This is what I wanted. Thanks!
Sudar
You're welcome .
tuergeist
+1  A: 

If you are using Python for CPU intensive algorithmic tasks I suggest use Numpy/Scipy to speed up your numerical calculations and use the Psyco JIT compiler for everything else. Your speeds can approach that of much lower-level languages if you use optimized components.

Salim Fadhley
I am not using much algorithmic tasks for this problem, but anyways thanks for the suggesting Numpy.
Sudar
+2  A: 

If you really need to write fast code in a language like this (and not a language far more suited to CPU intensive operations and close control over memory usage such as C++) then I'd recommend pushing the bulk of the work out to Cython.

Cython is a language that makes writing C extensions for the Python language as easy as Python itself. Cython is based on the well-known Pyrex, but supports more cutting edge functionality and optimizations.

The Cython language is very close to the Python language, but Cython additionally supports calling C functions and declaring C types on variables and class attributes. This allows the compiler to generate very efficient C code from Cython code.

That way you can get most of the efficiency of C with most of the ease of use of Python.

Kylotan
Cython, looks good. Will give it a try.
Sudar
+1  A: 

I'd be wary of trying to measure just the memory consumption of an object graph over the lifecycle of an application. After all, you really don't care about that, in the end. You care that your application, in its entirety, has a sufficiently low footprint.

If you choose to limit your observation of memory consumption anyway, include garbage collector timing in your list of considerations, then look at ruby-prof:

http://ruby-prof.rubyforge.org/

Ciao,
Sheldon.

sheldonh
A: 

(you didn't specify py 2.5, 2.6 or 3; or ruby 1.8 or 1.9, jruby, MRI; The JVM has a wealth of tools to attack memory issues; Generally it 's helpful to zero in on memory depletion by posting stripped down versions of programs that replicate the problem

Heapy, ruby-prof, bleak house are all good tools, here are others:

Ruby

http://eigenclass.org/R2/writings/object-size-ruby-ocaml

watch ObjectSpace yourself http://www.coderoshi.com/2007/08/cheap-tricks-ix-spying-on-ruby.html

http://sporkmonger.com/articles/2006/10/22/a-question

(ruby and python) http://www.softwareverify.com/

Gene T