Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recoding PHP scripts in C - much speed difference? 2

Status
Not open for further replies.

Maccaday

Technical User
Dec 9, 2003
71
GB
Hi,

I'm setting up a project where speed of execution of scripts/functions is very important. I will initially be writing everything in PHP, and will be caching compiled scripts both on the hard disk and in memory to speed up execution. Much of the code will be re-used in lots of places all over the project, so I was wondering if it was worth re-encoding some of the most used functions in C.

I realise that when compiled, most scripts will end up being pure C anyway (unless you've fiddled with the engine, and have added C++ support or something), so I was wondering if there is any point in re-coding. If a function only uses extensions and other functions that are based on C, then would re-coding the function in C make any speed difference to the execution of the function if it is cached in a compiled state anyway? Does this perhaps depend on the function, which could be written more efficiently in C?

Thanks in advance for any responses.
 
Compiled code generally runs faster than interpreted code, which is what PHP is. If you have something that takes a lot of time to process, then compiling it could speed things up. There are ways to optimize loops (one of the worst offenders for consuming time) like unrolling them.

Compiled code isn't particularly portable, which means you can't move something compiled for a Microsoft operating system to another OS. If that's not an issue, then making sure the compiled code and interpreted code can communicate would be the main thing. The first thing I'd try, though, is optimizing the script for speed, and then if you want to go to compiled code, you have a good idea what will be the fastest for that, too.

Lee
 
Lee,

Thanks for your response. I'm not sure that I made my question very clear, though - sorry.

I realise that compiled code is faster than interpreted code, and that PHP is an interpreted language. However, if you cache scripts and functions in memory/on the hd, then the code is stored in its compiled state. This will be at the same level (i.e. computer-code) as the extenstions written for PHP (since they are also compiled).

Certainly code-optimisation is important, and I'll be looking at that a long time before I were to do any C-coding, but what I'm asking is: if I were to re-write the code using C instead of PHP (i.e. as an extension, probably using a similar syntax to the PHP scripts), would this be any faster at runtime compared to compiling the PHP scripts and storing them in a cache?

My instinct is that some will be the same, whilst others will run quicker if written using C (since there are probably some functions/features in C that are not available in PHP that could speed things up - my knowledge of C is minimal, so I don't know this for sure yet).

(I'm not particularly fussed about portability, though I'm not sure how difficult it is to port C code written for Unix-based systems to Windows-based systems.)

Cheers.
 
I take it you're not doing web scripts here, but administrative type scripts. I've not used PHP for that (yet). I don't know what kind of machine code the PHP interpreter converts the instructions to, though I'd guess that C would be faster most of the time. The more flexible a language is when writing it, the more code has to be generated to handle things. That's what makes the BASIC languages easy to write, but compiled they create larger and slower programs.

If you're working with arrays in PHP, then I'd say that C would probably be a lot faster because the programmer has to deal more explicitly with memory management.

Lee

 
The interpreter will generally convert the PHP code to C at runtime. The Zend Engine (which is the core of PHP), and almost all extensions to PHP are written in C, and PHP is (as far as I'm aware) converted to compiled C. These extensions are obviously compiled before they can be used as part of PHP.

An exception to the above will be if one or more of the extensions used in a script uses another language, probably C++ (which is essentially C with a few modifications and some additions). I know that it's possible to write PHP extensions with C++, but I think you have to do some fiddling to the core PHP files. It is probably even more difficult or impossible to have PHP extensions in other languages, due to the incompatability with C as a machine code.

Since PHP code, when interpreted, is compiled C code, and PHP extensions are compiled C code, with most scripts I would imagine there not to be much of a speed difference. The thing that slows down the process is the compilation, which with PHP is by default performed on every execution of a script. However, if you cache the compiled script, then you aren't compiling it each time, and you have a stored copy of the machine code. This removes the usability/flexibility->slower speed issue you mention.

The scripts will be doing a whole host of things, including many website-based tasks. The kinds of things that I'm thinking of writing as C extensions are a database abstraction function, a memory caching function, a hard-disk caching function, and many other scripts that will insert XML into documents. These functions will be fed some values, and will create the XML from them. Once written, and checked for errors, these functions will not change, but the inputs will vary greatly - so the outputs will.

Because of the fact that it is far easier to write scripts in PHP than it is to write extensions in C, I wouldn't think of re-writing any of the PHP scripts until the 'final' version had been established (i.e. one that wasn't going to change, and was 'future-proof'. However, if there is going to be very little difference in speed (after caching), then I quite possibly wouldn't even bother.

Cheers.
 
PHP source code is never converted to C. It is converted to an intermediate pseudo-machine code (some kind of old good P-code) then interpreted by Zend Engine.
Pure interpreters (from the sources) are obviously slower than compiled code (50-500 times). Intermediate code coefficient (as usually) is 10-50.
If a script executes many library functions calls (regular expressions, file/db i/o), the coefficient may be 2-5 or less.
C codes (for CGI scripts too) may be absolutely portable. It depends on programmer's skill (and project specifications and management efforts).
 
ArkM > Thanks for the clarification. I will almost certainly consider re-writing some parts in C in that case.

Trollacious > It would have been even better if it had been correct! ;-) It just goes to show that you should never assume things you think are true. The rest is still true (I think).

Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top