by toolslive · 5y ago · 0 comments
https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
it's faster than python, but my grandma is too and she died in 1993. However, function calls are extremely slow. If you would bench something like a naieve fibonacci implementation (or ackermann's function) you would be surprised.
<?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. ) 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.
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.
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)
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.
https://twitter.com/ArkadiuszKondas/status/11196548025800212...
PHP 8.0.12:
$ time php fib.php 42
42 433494437
real 0m29.274s
user 0m29.003s
sys 0m0.105s
$ time php -dopcache.enable_cli=1 fib.php 42
42 433494437
real 0m26.341s
user 0m26.179s
sys 0m0.058s
----------------------------------------
PHP 7.3.5:
$ time php fib.php 42
42 433494437
real 10m23.573s
user 10m16.592s
sys 0m1.828s
----------------------------------------
Settings for PHP 8:
PHP Version => 8.0.12
auto_globals_jit => On => On
pcre.jit => 1 => 1
opcache.jit => tracing => tracing
opcache.jit_bisect_limit => 0 => 0
opcache.jit_blacklist_root_trace => 16 => 16
opcache.jit_blacklist_side_trace => 8 => 8
opcache.jit_buffer_size => 0 => 0
opcache.jit_debug => 0 => 0
opcache.jit_hot_func => 127 => 127
opcache.jit_hot_loop => 64 => 64
opcache.jit_hot_return => 8 => 8
opcache.jit_hot_side_exit => 8 => 8
opcache.jit_max_exit_counters => 8192 => 8192
opcache.jit_max_loop_unrolls => 8 => 8
opcache.jit_max_polymorphic_calls => 2 => 2
opcache.jit_max_recursive_calls => 2 => 2
opcache.jit_max_recursive_returns => 2 => 2
opcache.jit_max_root_traces => 1024 => 1024
opcache.jit_max_side_traces => 128 => 128
opcache.jit_prof_threshold => 0.005 => 0.005Let me give you an example;
https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/regexredux-python3-2.html
and https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/regexredux-perl-4.html
The Python code uses ctypes, thus the unfair marginal difference.I have seen the same unjust comparison with the majority of the examples and decided to stop relying on this page for validating programming language performance.
No they do not.
Let me give you an example:
https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
*** You ignored the Python code that does not use ctypes.
> I have seen the same unjust comparison with the majority of the examples…
No you have not.
There are 10 different tasks.
pidigits accepts the use of third-party libraries (GMP)
regex-redux accepts the use of third-party libraries (PCRE)
That's 2 out of 10.