Why write your own extension?
Even though we all know PHP is faster than a speeding ticket, can it be made to go faster? C programmers have for years trumpeted the fact that their language is extremely fast and therefore capable of handling performance-critical tasks. However, very often you will find that when C programmers really need performance, they use inline assembly code.
Author's Note: If you are running on Linux, open up your Linux kernel source (probably in /usr/src/linux), and pick what you consider to be a CPU intensive operation. I chose arch/i386/lib/mmx.c, the code that handles MMX/3dNow! instructions on compatible chips. Inside this file you will see lots of quite complicated C code, but also extended instances of assembly code wherever speed is optimal. In fact, if you change directory to the root of the Linux kernel tree, try this command:
grep "__asm__" ./ -rl | wc -l
That searches all the files in the Linux source distribution for instances of assembly, and counts the number of files that match. In 2.5.65, which has been superceded numerous times now that the 2.6 kernel is stable, the number of files in the kernel source that use assembly once or more is the rather ominous number of 666! So, C programmers using assembly is quite a widespread thing.
PHP programmers, although blessed with a naturally fast language, can also use a lower-level language for speed critical operations - although in our case, C is next on the food chain. While it is possible to use assembly code from PHP (through C, as C programmers do), there is more than enough speed improvement just switching to C, so that is what I will be covering here.
Please note that, within the extent of the space available, this is a no-holds-barred chapter - prior knowledge of C is required, knowledge of assembly would be good, and very good knowledge of PHP is mandatory.
Next chapter: The C Perspective >>
Previous chapter: Writing extensions
Home: Table of Contents