views:

79

answers:

3

My PHP app has a number-crunching part that is just to slow for PHP, so I was thinking of building a custom C extention, but it is impossible to find any good reference to start with :(

Is there a guide on how to do something like this?

+1  A: 

This might not be an answer but more like a suggestion, There are tools out there to compile your php to an executable, which you could just then use as an extension. This Would uniform your code a bit and unify your project. I have tried something like this a while ago. The compiled php acts no differently than the compiled c would.

Babiker
Yes, you can use PHP's embeded SAPI. Extending and Embedding PHP covers that. But he specifically says he has a web app with a portion he wants to optimize, so I guess that wouldn't be what he wants.
Artefacto
@Artefacto, thanks. What your commented makes sense, but i would think compiled php would be alot faster than its interpreted version.
Babiker
+5  A: 

The best resource, although outdated in several aspects (it only covers PHP until version 5.1) is Extending and Embedding PHP by Sara Golemon. Even more outdated is the PHP documentation. On the other hand, the content on the PHP wiki is very up-to-date, but also quite incomplete and not very oriented for beginners. See also these articles, part V of Advanced PHP Programming by George Schlossnagle, chapter 14 of Programming PHP by Rasmus Lerdorf and Kevin Tatroe and, specially, these slides.

Finally, the most authoritative source you'll find is the source code of the extensions bundled with PHP.

Artefacto
A: 

Another option would be a command line tool written in C, that you start from PHP and communicate with over stdin/stdout. In many cases this is much easier to write and to deploy, but it ultimately depends on your use case.

Eiko
This is also generally slower.
fmark