Integrating C in web apps

I'm designing a web app that generates imagery based on lots of spatial data, processing up to 100k data points per image. Ideally, this will happen in something close to realtime. I've implemented this in PHP with GD, and it takes about .6 seconds for the worst case, way too slow.

I'm still tweaking the algorithm, but what most people have told me is to get closer to the hardware and write the CPU-intensive part in C. Makes sense, and I can write the C code, but I'm not sure about how to make it fit with the rest of the site, which is written in PHP. Should I call it directly as a CGI program? Would it work to invoke it as a system() call from PHP? The latter would be more convenient for me, but I don't know if would be a performance hit.

I'm running Apache 2.x. Thanks for any advice.

5 Replies

A third option is to write your own PHP extension. This will give you the performance of compiled native code, without the hassle of calling an external application.

For scientific and engineering work, I call C and C++ from within Python as a compiled extension - definitely far faster than any other method, and compute speeds are very high for well-written C/C++ routines.

James

Wow, thanks to both of you - I didn't know that was an option. Looks like it's definitely the way to go. I found a guide— http://devzone.zend.com/node/view/id/1021

Guess I'll roll up my sleeves and try to build this thing. Any tips from those who have done this before?

Doesn't mod_php would give you the benefits of pre-compilation? That would surely be the easiest.

@Xan:

Doesn't mod_php would give you the benefits of pre-compilation? That would surely be the easiest.

Perhaps easiest, but you'd still be writing the algorithm in a highly-abstracted (slow) language. Much faster to do it in C.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct