5 ms·
I had to laugh at one line. PHP or Perl are called interpreted because the interpreter (installed on the destination machine) compiles each line of code as it
by btilly 6y ago
I had to laugh at one line.
PHP or Perl are called interpreted because the interpreter (installed on the destination machine) compiles each line of code as it goes.
Yeah, that hasn't been true of most "interpreted languages" in decades. The most common interpreted languages that I can think of where you parse as you go are shells like bash.
Languages like Perl and PHP are called interpreted because an interpreter runs the script. But the interpreter actually compiles the code first and then runs off of the compiled representation. Which is byte code - just like the JVM. If you put energy into it, you can use all the same techniques that the JVM does. As a practical example, both nodejs and pypy (a fast variant on Python) have a JIT. Julia regularly hits speeds for numerical calculations that is fully competitive with C++ and FORTRAN.
- mfontani 6y agoA tangible example: if you've got a script the likes of: #!/bin/bash echo 'Hi!' sleep 20 echo 'Hello!' ... and runt it ... and while the script is executing the sleep line, modify it so that instead of 'echo "Hello!"' it does something else, like 'echo "Oops!"'... ... once it resumes from the sleep, it'll execute the updated code. Don't ask me how I know... I just wish it were more well-known. Don't modify shell scripts while they're running!
- pkulak 6y agoThat is amazing. Bash doesn't even load the script into memory? It just keeps a line pointer and chunks through the open file? There's something comforting about that. :D
- CodesInChaos 6y agoI think batch files do a linear scan for the target label every time the goto is executed.
- ww520 6y agoThe difference is the “compiled representation” of Java is done ahead of time into the .class files while the other two are being done runtime.
- johannes1234321 6y ago> while the other two are being done runtime. On a common PHP setup (with OpCache today, any predecessor in the past) this happens only once on startup, thus is neglectible. (Java's VM and JIT are faster, no doubt, but where the translation happens matters only a little ... especially since Java does optimisations and JITting on the VM at run time only as well)
- jayd16 6y agoThe PHP Wikipedia page seems to argue that the most popular implementation, Zend, is an interpreter. https://en.wikipedia.org/wiki/PHP#Implementations https://en.wikipedia.org/wiki/PHP#Implementations
- btilly 6y agoIt argues that it operates as an interpreter, in exactly the way that I described. From your link, The Zend Engine compiles PHP source code on-the-fly into an internal format that it can execute, thus it works as an interpreter. In other works at startup itreads code, compiles to an internal form of byte code, and from then on it executes that. So it works as an interpreter (you give it code, it runs). But internally it does not go line by line as you go. As a simple demonstration, a syntax error on line 10 of a PHP script will keep it from executing code on line 5. By contrast a syntax error on line 10 of a Bash script will not keep it from having executed line 5 first.
- girvo 6y agoYou can examine and dump the byte code from the runtime with phpdbg — it basically acts as a compiler to byte code on startup. Of course because it’s also very dynamic, the lines are blurred, imo