Live data from Hacker News

How come PHP seems so much faster than Rust?

reddit.com

61–70 of 116 posts

Re: How come PHP seems so much faster than Rust?

#61

Earlier quoted context omitted.

>for a long while [PHP] was the only viable language for developing web applications. That is not what I remember. CGI existed before PHP (1994), and was used extensively for web apps (ebay still appears to use it to this day). Also, mod_perl and Java Servlets were introduced around the same time, circa 1996, so I don't think it's correct to say PHP had a monopoly on web apps "for a long while".

Coldfusion was also a thing around that time. Flash sites as well.

Yes, Flash was huge. When I was learning Perl around 2000 I shared a flat with a Flash animator who worked for an agency with huge clients such as Sky. It was all done in Flash for very big dollars.

Re: How come PHP seems so much faster than Rust?

#62

The 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?…

> … "PCRE", but the ones with Perl in name…

fyi "Regular Expressions (Perl-Compatible)"

https://www.php.net/manual/en/book.pcre.php

Re: How come PHP seems so much faster than Rust?

#63
post #9

Random 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-l...

> And PHP might

The program source code is shown.

Re: How come PHP seems so much faster than Rust?

#64
Obviously, 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... 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

Re: How come PHP seems so much faster than Rust?

#65
post #11

While 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)!

PHP isn't popular because it's easy, it's popular because for a long while it was the only viable language for developing web applications.

Delphi, C++, VB have been viable since 1995, Smalltalk since 1995, Objective-C since 1996, Python since 1998, Java has been viable since 2001, C# and VB.NET since 2002.

PHP only became viable with version 3, around 1998, and having most ISP only offering a choice between CGI (usually C or Perl) and PHP, naturally drove most cost oriented devs to PHPs.

Those willing to shell out more, could use one of the above options.

Re: How come PHP seems so much faster than Rust?

#66
post #37

Earlier quoted context omitted.

While your observation is true to some degree, I do remember ASP, Cold fusion, CGI and a few other more obscure technologies way back when. Perhaps I was quite late to the party though - I only started programming in 1999 (got a computer for my 11th birthday!) - so I'm not sure what it was like before then.

> I do remember ASP, Cold fusion, CGI This is the competition that PHP was so much better than.

CGI yes, ASP certainly not.

Re: How come PHP seems so much faster than Rust?

#67
post #11

While 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)!

The same is true for python. I spent some time trying to write a CSV parser in guile scheme that was as fast as python's (mistakenly believing that python's CSV parser was actually written in python). I wrote a fast-ish streaming FSM-parser (about 100loc) but only managed to be about 1.7x as fast as python's CSV module. Browsing the python source I realised that the python parser was written in optimised and nicely buffered C.

I could probably compete with python if I used guile's lower level C-procedures, but I sort of lost interest after that :)

Re: How come PHP seems so much faster than Rust?

#68
post #62

The 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?…

> … "PCRE", but the ones with Perl in name… fyi "Regular Expressions (Perl-Compatible)" https://www.php.net/manual/en/book.pcre.php

Thanks for letting me know.

Re: How come PHP seems so much faster than Rust?

#69
post #20

Earlier quoted context omitted.

I 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 .

It’s really easy to write PHP, which means that people who don’t know about escaping input write PHP. While there are some web-focussed languages (vs frameworks) that deliberately make it hard for you to write that sort of code, barely anyone uses them.

"escaping input" is a notorious delusion. And a bad one.

- it is not "input" must be treated, but, so to say - output. It's the destination that matters, not the source. By the time you get the input, you have no idea how and where it will be used. You can tell it right before the output (or, rather, better to call it "use") only. Say, you've got some input that didn't pass the verification and you will have to display it back in the HTML form. And if you already "escaped" it for SQL, it will be malformed. And vice-versa. Moreover, any data must be formatted properly before use, not just something that you tagged as "input".

- the word treated above is for a reason. Because the term "escaping" suggests some certain routine in PHP, which is the actual reason for numerous SQL injections.

Re: How come PHP seems so much faster than Rust?

#70
post #5

This 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 ju…

[deleted]
Post reply on HN