Yes, PHP is faster than C#
withinboredom.info
withinboredom.info
* The C# version invokes EndOfStream up to 4096 times for every 1 call to feof in PHP version.
* The C# version opens the file in text mode (and thus does UTF-8 decoding) whereas the PHP version opens the file in binary mode.
To read in binary, you open with 'rb' and not just 'r'
> To read in binary, you open with 'rb' and not just 'r'.
No. "The default translation mode is 'b'." [1] And even in 't' mode, it's still reading bytes in PHP vs UCS-2 characters in C#.
int StreamReader.Read()
int StreamReader.Read(char[] buffer, int index, int count)
https://github.com/dotnet/runtime/blob/1ba0394d71a4ea6bee7f6...https://withinboredom.info/blog/2022/03/16/my-favorite-langu...
> Like, look at this Merge Sort in C# [1] (130 lines) and then look at it in PHP (31 lines) [2]. You can’t beat that kind of productivity, not to mention in PHP, you can even mix data types (like ints and floats) where in the C# version, it’d be much more difficult.
[1] https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorit...
[2] https://www.w3resource.com/php-exercises/searching-and-sorti...
The C# version is the first Google result for "c# merge sort". But a later result is from w3resource.com, which where the PHP code came from. That C# code is only 55 lines and could easily be shorter by replacing loops with standard library calls:
https://www.w3resource.com/csharp-exercises/searching-and-so...
Also, both versions of the code from w3resources.com are trash -- they use an O(N) step to remove an element from the front of the array (array_slice in PHP and List.Remove in C#), which makes them O(N^2 log N), which is even worse than a naive selection sort or bubble sort.
using var file = File.OpenRead("file.bin");
var counter = 0;
var sw = Stopwatch.StartNew();
var buf = new byte[4096];
while (file.Read(buf,0,buf.Length) > 0)
{
foreach (var t in buf)
{
if (t == '1')
{
counter++;
}
}
}
sw.Stop();
Console.WriteLine($"Counted {counter:N0} 1s in {sw.Elapsed.TotalMilliseconds:N4} milliseconds");My C# is very rusty (no pun intended) but I would guess the core of the program could be something like
File.ReadAllBytes("file.bin").Where(x => x == '1').Count()
And nitpick: the code you gave has a bug. file.Read can return less than 4096. If so, you should only loop over the part of the buffer it filled.(This bug won't affect the performance comparison, but I was just reminded of how error prone these kinds APIs can be vs the PHP/Python route of having the library function just allocate a new buffer each time.)
int len;
while ((len=file.Read(buf,0,buf.Length)) > 0) {
for (int i=0; i<len; i++_) {
if (buf[i] == '1') {
counter++;
}
}
}Wow, that's a claim I've never seen before. What's it based on? (As an example, I'm wondering if you may not be aware that there are several static analysis tools for PHP.)
https://cacm.acm.org/magazines/2017/10/221326-a-large-scale-...
Also, I used to program in both and known issues within the PHP language would make my development so much harder. But it's been pointed out to me that PHP has improved over the years so it might not be as difficult as before.
Edit: To clarify it’s buffered in C# too but the difference is that it’s fetched one byte at a time via a method call per byte. Not an I/O call per byte (then the difference would have been much larger of course).
> “But Rob,” I hear you say, “they’re not reading it byte-by-byte in the PHP version!” and I’d reply with, “but we’re not reading it byte-by-byte in the C# version either!”
C# also reads in 4K chunks: https://github.com/dotnet/runtime/blob/1ba0394d71a4ea6bee7f6...
The read() calls (which most of the time just increment the index into the buffer and returns what’s there) are still made.
The usual way of doing what he’s trying to do will look exactly the same in C# as in his php version, with explicit buffering.
In the latest addendum on the blog post, I ended up removing the counting of 1s.
2.Programming languages are more then their syntax, they are entire ecosystems. 99% of the time its not the syntax/features/paradigms that make you move fast its the libraries