PHP vs C CGI for small web services

I am creating a small web service that only machines will access, not users who just take the query string and make a few MySQL queries. I decided to write this in PHP because it is simple and easy to write and works well. My boss, however, wants us to write it as CGI in C (using FastCGI) because he says he will be faster and use less memory. I am not very keen on this idea for several reasons:

  • The MySQL API for C seems to have a lot more calls than the equivalent PHP, and requires much more error handling.
  • String handling in C is somewhat complicated and messy.
  • The code in C is almost 3 times longer than the equivalent code in PHP and looks pretty dirty with a lot of error handling.

But this is just my opinion. What other factors need to be considered? Is C the best tool for this job? Or PHP?

+6
source share
8 answers

Some points about this:

  • It’s much easier to implement any service network in PHP rather than C, because PHP is a language designed to work online and C is a common 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 equipment is very limited), you can use PHP accelerators that compile the source code ( for example )
  • Access to databases is equally punished in both languages, so if your program needs to access the database, it will have the same performance.
  • The more important: the boss is the boss :)
+2
source

If the speed of your (or your boss) is a concern, check out the G-WAN server , which allows you to write C scripts. There are several examples of MySQL in the forum . It will be much faster than FastCGI (which must cross process boundaries through sockets).

+3
source

IMHO, if most of the processing is done by MySQL, there is no need to write this code in C, because the difference is not significant, but if there is a lot of processing in your code, it makes sense to listen to your boss and make it in C

+1
source

Both tools are fine. If your boss wants C, made him on C.

0
source

You have no choice, because your boss wants it. If you cannot encode in C then just C it.

0
source

Ask your boss if it would be better if you used C ++.

  • Remind him of the maintenance of the code, and with C ++ you can introduce fewer errors at a price of slightly lower speed.
  • Remind him that the “time” necessary to fix or improve the software is “included” in the “runtime” - the time when you will have services, due to a small error.

And try fastcgi ++ . You also have boost and other libraries that can help you make boring stuff faster and focus on WHAT MATTERS.

And if the application performs a lot of calculations, it is recommended to do it in C / C ++.

  • Otherwise, remind your boss that, as a rule, regardless of the language, the application will not be slowed down by calculations, and “waiting for resources” - waiting for a disk or waiting for a database are the most common, which happens regardless of language.
  • Remind your boss that for speed you can get a 10-60% improvement using apc .

If he just doesn’t want, you have two options:

  • quit assignment
  • do as he asks you

Why C ++, not C

It is true that with good coding conventions, you can also write managed code in C.

But with C, you still have the verbosity of error handling, as opposed to exceptions, zero-terminated strings, cards, or whatever. I love C far more than C ++, but let him take a look: this is not about the language, but about the mentality of the despot (boss), who needs to deal with arguments and learn to listen to his employees.

0
source

Your application is most likely related to I / O. Thus, it will not be noticeably faster in C.

0
source

shudder ... you must leave the company. The boss should not be allowed to obey the workers ... unless he is also a programmer in the company’s guru :)

-1
source

Source: https://habr.com/ru/post/891792/


All Articles