Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP vs. C CGI for small web service

I am creating a small web service which will only be accessed by machines, not users that simply takes a query string and makes a few MySQL queries. I decided to code this in PHP because it is simple and easy to write and does its job well. My boss however, wants we to write it as a CGI in C (using FastCGI) because he says it will be faster and use less memory. I'm not so keen on this idea for a few reasons:

  • The MySQL API for C seems to have a lot more calls than the equivalent PHP and need a lot more error handling.
  • String manipulation in C is somewhat complicated and messy.
  • The code in C is almost 3 times as long as the equivalent code in PHP and looks rather messy, with lots of error handling.

But that's just my opinion. What other factors do I need to take in to account? Is C the best tool for this job? Or is PHP?

like image 628
DanielGibbs Avatar asked Jul 01 '11 06:07

DanielGibbs


3 Answers

If speed is your (or your boss's) concern, check out the G-WAN server, which allows allows you to write C scripts. There are some MySQL samples in the forum. It'll be much faster than FastCGI (which has to cross process boundaries via sockets).

like image 145
Lumi Avatar answered Oct 20 '22 02:10

Lumi


Some points about this:

  • It's much easier to implement any web service in PHP rather than C, because PHP is a language designed to work online and C is a general language.
  • C is compiled and this makes it much faster than PHP programs, but if you are really worried about optimization (your program consumes a lot of resources and your hardware is very limited) you can use PHP "accelerators" that compiles the source (for example)
  • Access to databases is equal penalized in both languages, so if your program has to access a lot to DB, it will have a similar performance.
  • The more important: the boss is the boss :)
like image 2
Ivan Avatar answered Oct 20 '22 02:10

Ivan


IMHO, if most processing is made by MySQL, there is no need to write this code in C, because difference is negligible, but if there is much processing will be made by your code, it makes sense to listen to your boss and make it in C

like image 1
Greenisha Avatar answered Oct 20 '22 04:10

Greenisha