Author Topic: Python speed  (Read 2434 times)

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Python speed
« on: March 27, 2011, 10:40:42 AM »
For those of you who use python how does it do in computation heavy applications?  At work I'm running into some speed issues with PHP and my current project.  The current project requires a lot of computation in some tight loops.  My first alternative is C++ but I wanted to see if there were some other options.

Dumah

  • Jackass IV
  • Posts: 960
  • Karma: +21/-6
Re: Python speed
« Reply #1 on: March 27, 2011, 02:05:38 PM »
Not too great as most benchmarks will tell you. There are projects like Psyco that increase speed.

My preference is to write the fast stuff in C++ then compile it into a python module using boost::python. I recently had to do this in a project that used sqlite - the python module for this is great but some of the admin api is only available in C, so I wrote a module, linked in boost and it worked like a dream

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Python speed
« Reply #2 on: March 27, 2011, 11:11:02 PM »
Hmm, I wonder if I can do something similar with PHP.  One part I want to avoid is dealing with MySQL stuff in C++.  It is much easier in PHP.

webwhy

  • Jackass IV
  • Posts: 608
  • Karma: +15/-10
Re: Python speed
« Reply #3 on: March 28, 2011, 12:01:40 AM »
i use SWIG for interfacing with libraries written in C/C++.  I build ruby interfaces, but it supports PHP and Python.

http://www.swig.org/

The php mysql routines are thin wrappers around the mysql C client library.  I doubt you would be able to speed up the database code much at all.

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Python speed
« Reply #4 on: March 28, 2011, 07:24:23 AM »
i use SWIG for interfacing with libraries written in C/C++.  I build ruby interfaces, but it supports PHP and Python.

http://www.swig.org/
Thanks, I'll take a look.

Quote
The php mysql routines are thin wrappers around the mysql C client library.  I doubt you would be able to speed up the database code much at all.
Yeah.  I remember doing MySQL in C and I much prefer to do them in PHP.

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Python speed
« Reply #5 on: March 30, 2011, 12:54:21 PM »
So took a look at swig and I don't really like all the special syntax required.  Especially syntax that isn't in comments (so you can't compile it as a C++ binary if you wanted)

webwhy

  • Jackass IV
  • Posts: 608
  • Karma: +15/-10
Re: Python speed
« Reply #6 on: March 30, 2011, 01:55:05 PM »
So took a look at swig and I don't really like all the special syntax required.  Especially syntax that isn't in comments (so you can't compile it as a C++ binary if you wanted)

There is a special syntax, but it's not very complex, and I'm not sure I understand your second point.  The swig directives are either ignored by the C preprocessor (using an #ifdef) or declared in another file altogether, which is how I've always done it

The last project I used it for was to interface with a proprietary OCR library using Ruby.  I build a small C++ wrapper around my client's C library that defined the interface I wanted to call from Ruby.  I used swig to generate the ruby bindings between my C++ wrapper library and the ruby library.

My point is that you should compile the wrapper library separately from the core library, and it should build without swig.  There is no hard dependency on the core C/C++ code on SWIG if its done correctly. 

I think it's easier than the alternative.

Dumah

  • Jackass IV
  • Posts: 960
  • Karma: +21/-6
Re: Python speed
« Reply #7 on: March 31, 2011, 04:23:49 AM »
Can vouch for it and its not official Boost but - http://www.ohloh.net/p/boost-php