7

I want to use assembly code to handle some critical tasks concern with performance using PHP. How can I do this? I have searched before but nothing at all.

Jack Cood
  • 301
  • 3
  • 10
  • Assembly code? PHP is an interpreted language. More information here: http://stackoverflow.com/questions/1514676/is-php-compiled-or-interpreted – H2ONOCK Oct 22 '13 at 08:35
  • 1
    No. You don't really want to do that. Moreover, **you can't**. If you need performance, design a proper architecture in your platform. Use internal services for the critical tasks, and leave the PHP handling the client's requests. – opalenzuela Oct 22 '13 at 08:35
  • 3
    [It is impossible.](http://stackoverflow.com/questions/10761931/is-possible-to-include-an-assembly-code-to-php-script) – vvns Oct 22 '13 at 08:37
  • PHP is a pre-processor. You could earn serious cred by writing an Assembler in PHP, but assembler, see the extension answer below folks, you could embed assembler in the C, but it would be platform dependent then. – mckenzm Nov 16 '21 at 03:10

2 Answers2

4

I believe you are talking about extending PHP.

You can do that by creating custom extensions in c language. A simple tutorial on how to do that you find here

Udan
  • 5,429
  • 2
  • 28
  • 34
  • @Udan : In my case, I'd like to create a php program for arbitrary just in time compilation, Is there an alternative to call gcc ? Are there really no equivalent of the python ctype function which allows to dlopen() any shared object files ? – user2284570 Nov 15 '16 at 22:38
  • take a look at this question http://softwareengineering.stackexchange.com/questions/29344/jit-compiler-for-c-c-and-the-likes. I'm sure you will be able to find a clever way to use a JIT compiler inside a php extension if you really need it. – Udan Nov 16 '16 at 12:42
  • Looks like the "here" link is broken. – rcgldr Mar 31 '20 at 16:59
2

Using assembly for your performance problems, is like using a sledgehammer to open a dam, while there is probably a lever somewhere, that opens the dam. Better look at your structure first and see why those critical tasks have performance issues and see if you can alter the strucure.

Besides that, as php is a scripting language that is interpreted on run-time, rewriting the code to C, like in the example from Udan gives 90% of the performance boost, rewriting the C code to assembly might add some extra 10%, but will take 90% of the time.

On the other hand, the argument you are using assembly in your code gives you some brag-rights, just up until the moment another programmer looks at your code, restructures it giving a 50% performance boost and completely bypasses your assembly.

Alfons
  • 511
  • 4
  • 17
  • -1 - using assembly for web development is possible and easy. And the performance gain is much more than 10% Your 10%/90% analogy is biased are opinion based. – johnfound Oct 22 '13 at 09:19
  • 1
    +1. In terms of performance gain per coding hour, for tasks more complex than "Hello world", C beats assembly any day. And that's discounting maintenance costs. – Seva Alekseyev Oct 22 '13 at 16:00
  • @johnfound fancy using PHP for web and not for preprocessing code! I would never have thought of it. Also assembly is not terribly portable so a future migration may be impacted. – mckenzm Nov 16 '21 at 03:15