9 ms·
How come PHP seems so much faster than Rust?
- aussieguy1234 7y agoWhen you use any functions or classes any of the PHP standard library, the underlying implementation is usually in C.
- fortran77 7y agoThis is an interesting discussion. It seems that it's possible for i/o to be slow in Rust, and the PHP regex engine may be very good....
- ericmcer 7y agoThis code is mostly just testing the regex engine?
- partoa 7y agoSeems to be the case. All the same, given enough cases, I would expect that one can find something at which most major languages are faster by a generally faster language.
- fortran77 7y agoIs the Rust regex engine slow because it's written in Rust?
- steveklabnik 7y agoYes.
- mlindner 7y agoNot really, it's mainly because it hasn't been optimized over many many years yet. Also, everything in Rust is implemented in Rust. Unlike interpreted languages like PHP, it's self-hosted.
- fireattack 7y agoThe top reply says "Don't underestimate PHP when regexes are involved." and linked a benchmark [1]. But in that benchmark apparently Rust is faster than PHP (The slowest "Rust #2" is 2.45 sec. while PHP is 2.80 sec. He also mentioned "PCRE", but the ones with Perl in name are even much more slower [the fastest being 14.95 sec.]). Did I miss his point by linking that benchmark, or did I interpret the benchmark wrong? [1]: https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/regexredux.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
- detaro 7y agoOne could expect PHP to be a lot slower, the benchmark shows it can keep up. PCRE is the name of the regex library used by (among others) PHP, which is why PHP is good at regex. Other comments address why the Rust program is not optimal.
- o_p 7y agoI guess the poster didnt saw Rust being faster, only that PHP is pretty fast, or at least not slow (so it should be underestimated). Id take that slowdown any day just not having to touch Rust though :^)
- 1_player 7y agoTo be fair, the benchmarks game PHP script does quite more work that just matching a regexp, like forking into multiple interpreters (which can't be that fast) and communicating through a message queue. So any speed difference between Rust's and PCRE/PHP gets lost among the noise.
- igouy 7y ago> … "PCRE", but the ones with Perl in name… fyi "Regular Expressions (Perl-Compatible)" https://www.php.net/manual/en/book.pcre.php https://www.php.net/manual/en/book.pcre.php
- fireattack 7y agoThanks for letting me know.
- kelnos 7y agoThis is a little silly. The two programs are so simple that it's probably just testing the speed of the regex engines used by the two languages. Since PHP uses PCRE (a C library that has been around for quite a long time), you'd expect it to be fast. I'm impressed that marshaling back and forth across the FFI boundary isn't causing more slowness, but perhaps it is, and the real issue is that rust's regex engine is just painfully slow. The only way to know for sure would be to profile each bit to see what dominates: the file I/O? The regex matching? The FFI? The printing to stdout? Also, looks like there is a rust wrapper for PCRE[0]; might be interesting to try it to get a more apples-to-apples comparison. [0] https://github.com/cadencemarseille/rust-pcre https://github.com/cadencemarseille/rust-pcre
- aksx 7y ago> This is a little silly. Is it though? I interpreted question the following way. "Rust is a tool in my tool box, when the reason for picking it over other tools isn't demonstrably true, what am i doing wrong?" I agree with the rest of your comment, just not with calling the OP or their question silly.
- beatgammit 7y agoThe reason for picking it is for memory safety as well as performance. If your problem involves a lot of regex and memory safety isn't as critical, then feel free to pick a language with good regular expression libraries. If regular expressions are a smaller part of your problem you're solving, looking at overall performance is better than benchmarks like this.
- gtirloni 7y agoI'd never know that if this post didn't exist, so I don't think the investigation is silly at all.
- smaccona 7y agoYour conclusion is correct, but the reason I take issue is that that’s not the thesis of OP. OP’s original subject/headline suggests that, or questions why, Rust is generally slow vs PHP, and uses a regex example as the “proof”. A person who didn’t read the Reddit comments, or who is less versed in critical thinking, may accept the example as a valid proof of the thesis, instead of simply noting the fact that Rust may not be as good at regex as it is at other things. If the Reddit subject was “Rust is worse than PHP at regex”, nobody would bat an eyelid and the resulting conversational threads would have a different tenor (and your conclusion would and should be the same).
- chubot 7y agoRandom guess: There will be some difference in the regex engine speed, because PCRE is very fast in common cases. But doing a single regex match is not that much work, so it probably boils down to I/O. And PHP might use extra buffering to get better throughput for the common "stream a file" scenario. Similar question with Python being faster than C++: https://stackoverflow.com/questions/9371238/why-is-reading-lines-from-stdin-much-slower-in-c-than-python https://stackoverflow.com/questions/9371238/why-is-reading-l...
- igouy 7y ago> And PHP might … The program source code is shown.
- TheCoelacanth 7y agoNot the source code for PHP itself, though.
- Thaxll 7y agoRust doesn't make an average code implementation fast. Even naive mistakes like buffering / allocation make things really slow.
- osrec 7y agoWhile this discussion boils down to the efficiency of the underlying regex engines for PHP and Rust, it does highlight (to me) why PHP is so popular: in the hands of average programmers, PHP is quick to write and fast (enough) to execute. I personally believe it's ubiquity is well-deserved, and I actually like it as a language despite all its quirks (which exist in almost every language I've come across)!
- Avamander 7y agoI don't like it from the security perspective, looking at just how many vulnerabilities PHP-based software has had, it's too trivial to create a XSS, SQLi, CSRF, LFI, RCE and many other classes of vulnerabilities. I think there are better options out there than on average give better results. Maybe it's just incredible selection bias I'm encountering \shrug.
- delfinom 7y agoBut plenty of those vulnerabilities are not the fault of the language but the frameworks written by others. You can just as easily create SQL injection attacks in a Rust program or Ruby program. You can just as easily introduce XSS/CSRF problems in any of them. Etc.
- beatgammit 7y ago> You can just a easily create SQL injection attacks in a Rust program I disagree, mostly on the grounds that string concatenation isn't very convenient, so it tends to be easier to do things "the right way" instead of hacking things together. Yes, you can do string concatenation using something like `format!(...)`, but at that point it's really similar to the correct solution that it's not really what you'd reach for by default. And yeah, the community here is a huge part of it, and IMO languages can and should be judged by their communities. If a language's community promotes insecure solutions, that's a language that I don't want to use at my company because I don't want my employees following that example.
- Avamander 7y ago
- jononor 7y agoI believe the Rust Regex Unicode support is opt-out, where as in PHP it is opt-in. Might make a significant difference.
- timvisee 7y agoCool. Never really noticed the non-Unicode variant would be faster. Here's some more info: https://docs.rs/regex/1.3.1/regex/#opt-out-of-unicode-support https://docs.rs/regex/1.3.1/regex/#opt-out-of-unicode-suppor...
- UK-Al05 7y agoI maybe wrong but rust uses a dfa regex engine, while pcre uses backtracking. So in theory rust's regex engine should be faster...
- roskilli 7y agoSeems like the flamegraph he took of his Rust program shows it using backtracking, maybe for the specific regexp he is using it's unable to use a DFA: https://i.imgur.com/9lx42Tu.png https://i.imgur.com/9lx42Tu.png
- contingencies 7y agoTheory. The php script and the log in question are probably in the kernel's aggressive block layer cache. The new files created by rust are potentially new and uncached, mean that it must be loaded from disk. Therefore the rust version is probably just losing time on disk access. Quick test: review whether or not the rust version runs faster on the second execution. Also or alternatively: use a profiler or copy the rust program to an in-memory block device (ramdisk) prior to access.
- nicklauri 7y agoObviously, because the PHP regex engine is written in C, not in PHP. It's more like Rust's regex crate vs C PCRE rather than PHP, or who calls system call faster. While Rust's IO is locked[1] and doesn't buffer (which is slower than buffered, unsynchronized IO like other languages). And regex crate is slow when capturing, which is used on the "benchmark". Reference: https://www.reddit.com/r/rust/comments/5zit0e/regex_captures_slow_compared_to_python/ https://www.reddit.com/r/rust/comments/5zit0e/regex_captures... 2 years ago, but it's still valid to me, I've just tested it and it's much slower as I expected. Feel free to correct me if I'm wrong :) [1]: https://doc.rust-lang.org/std/io/fn.stdout.html https://doc.rust-lang.org/std/io/fn.stdout.html
- alephnan 7y agoRegarding the slowness of Go in the benchmarks linked in the article, https://medium.com/@dgryski/speeding-up-regexp-matching-with-ragel-4727f1c16027 https://medium.com/@dgryski/speeding-up-regexp-matching-with.... There are tradeoffs with PCRE vs RE2 instead of just raw speed.
- stunt 7y agoIn regard to all bad criticisms PHP receives, I believe the language and ecosystem influence the way developers think. A bad language/ecosystem can dictate bad practices and bad design choices because that’s what the language encourages them to do. I remember every PHP-4/5 training course starting by mixing PHP and HTML. Majority of developers write crappy code even on a decent environment. But there is so much old and bad PHP code out there as a result of many years of teaching how to right bad PHP code that nothing can stop these bad criticisms forever. I don't write PHP at this moment but I know PHP7 made major improvements. PHP is still a very good choice for rapid web development and fits for many startups, small, and medium size business, and there are many successful examples to back it up.