Would it be better to use system functions rather than use a language?

There are many scenarios in which I questioned the performance of PHP with some of its functions and whether I should create a complex class to handle specific things using seemingly slow tools.

For example, complex regular expressions with sed and awk processing would seem to be exponential in performance, rather than a PHP regular expression and seemingly excessive parsing functions, and deal with this over time. If I had to perform many network tasks, such as searching for MX / DIGging / retriving, I would rather pass it through system() and let the OS handle it myself. PHP has too many functions that are inefficient and lead to slow pages or can be easily handled by the OS.

What are your opinions?

Do you think I have to do the hard work with the OS in my own / user functions?

+4
source share
5 answers

System calls can be very fast than using a solution built into PHP (although this is not always true, since the PHP functions themselves are built and compiled in C. Many of the basic functions and extensions of PHP are pretty fast).

Besides speed, the second factor is memory limitation. Externally called processes do not stop when the PHP restriction is per-w370>, which can be great when working with large files, for example.

In addition, some functions are simply not available in PHP itself. For example, it is not possible to simulate the ImageMagick feature set completely in PHP. The GD library does not come close to what ImageMagick has to offer.

The big, big minus is that with the help of system commands you effectively eliminate portability, which is part of the beauty of PHP. Moving an application to another server becomes a huge burden because the set of external commands must be identical - and this is not always the case, even on different Linux distributions, not to mention crossing the OS border on Windows or Unix Mac OS. I myself experienced problems with wget and ImageMagick in this regard, I am sure there are many more.

If you are working on a custom application for which you have complete control over the server environment (and deciding which servers will be purchased in the next five years), this may not be a problem. It will be one, though, if you create software that should be portable.

I personally tend to disable the feature (which will require an external dependency) rather than lose portability, but then I am very actively involved in creating portable applications. It really depends on your attention.

+3
source

Even if system processes are faster and less memory (extensive testing is required here), you need to remember something:

I would use system() calls with caution and use it only if you control the equipment on which your script will run. Using these calls may lead to the need to install additional software / packages and may not work (the same way) for all OSs, so if you cannot manage the server, I will stick with the PHP functions.

+1
source

I think that in fact it will be slower, because every time you call such a function, the OS starts a new process, and it takes a lot of time.

0
source

I would say if your program is designed to run in a shell using other external programs, such as sed / awk, excellent, because shellscripts also use external programs excessively, and the PHP script runs on the shell, the shell script, only in a different language. However, if it is a web application, it is better to do it in php - most public hosting environments do not allow the execution of external programs from php scripts.

0
source

(My experience is that "system calls" usually refer to calls to kernel operations, and not to calls to other programs - "pass it through system () and let the OS handle it" - it seems you think the same), but not one from the programs you are talking about are OS services - these are just other programs).

PHP is essentially a scripting language, which is usually just a glue for moving data between other programs, but there are some things to consider:

1) performance β€” using a new process can be costly

2) security - giving your web server unlimited access to all the programs in the system (even with limited permissions) is potentially very dangerous

3) keeping in mind (2) most configurations will prevent or limit what you can do

4) for large-scale development, this is quite dangerous - allowing programmers to write their code in any language of their choice, and then put PHP on top of it, then you will get an application written in many different languages

5) You can easily write your own custom PHP PHP extensions

If I were doing a lot of network tasks like searching MX / DIGging / retriving

Although I could believe that merging large datasets using awk / sed might be faster / more efficient than native PHP code, I find it a bit surprising that DNS lookups are faster using another client. How did you measure it?

0
source

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


All Articles