Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

(naieve fibonacci) like this:

    <?php
    
    function fib($n) 
    {
        if($n === 0) { return 1;}
        if($n === 1) { return 1;}
        $r = fib($n-1) + fib($n-2);
        return $r;
    }
    

    $n= (int) $argv[1];
    $r = fib($n);
    printf("%d %d\n",$n,$r);

just did some measurements on my laptop:

    | Language             |  n | time(s) |
    |----------------------+----+---------|
    | php (7.4.25)         | 42 |   422.0 |
    | python3              | 42 |    90.0 |
    | haskell (ghc -O3)    | 42 |    13.8 |
    | f#                   | 42 |     5.6 |
    | ts                   | 42 |     2.8 |
    | ocaml (ocamlopt -O3) | 42 |    0.98 |
    | C (gcc -O3)          | 42 |    0.66 |
(edit: added php version in table. )


This is what I get locally:

    time php fib.php 42
    23s

    time python3 fib.py 42
    1m1s
I suspect that you're doing something very wrong, like running PHP with an active debugger or similar.

If we're talking in comparison to slow interpreted languages like Python, then function calls in PHP are not particularly expensive. Debuggers can make them quite expensive though.

Edit: Just to note, PHP's execution time drops to 4s with enabled JIT.


Likely. I tried the benchmark and saw runtimes of over 5 minutes before realizing that XDebug was active and in develop mode. Adding `-d xdebug.mode=off` dropped it to 26s. That was with `opcache.jit` in the (default?) `tracing` mode. Turning it off didn't change the runtime, which is a bit puzzling. This is all with PHP8 on macOS, although I tried 8.1RC in docker and saw similar results.

Something doesn't quite add up across everyone's results here, but the extremely slow result at the top of the thread is almost certainly a debugger slowing things down.

Edit: I was missing `opcache.jit_buffer_size`, and saw the runtime drop to under ten seconds with that correctly configured.


I also get around 23s, PHP 7.4.5


How is php 422 seconds? Are you basing that off the return value of the function? FWIW, I get 57 seconds when I run your function with PHP 7.4 using microtime().


that was PHP 7.4.25

Yes this is a microbenchmark. Yes, I know binet's formula... Yes it's weird function calls are so slow in php.

with php 8.0 (jit enabled) you get something like 120s (for n= 42)


naieve and micro benchmarks is not meaningful in 95% of cases. Did you run on the CLI with -dopcache.enable_cli=1 for example?


Opcache avoids the compilation step. It's not relevant in this case (compilation ~0.01s out of ~400s).

Microbenchmarks are meaningful as long as one doesn't read too much from them. This benchmark shows that PHP seems to have a problem with recurrent calls.


Is JIT enabled and configured for PHP?




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: