> 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.
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:
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.
this is very naive reimplentation of the C# version. I managed to reduce the runtime of the same file from 5.7 seconds to just 800ms
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");
This code has a bug that can cause the count to be overreported. The last `file.Read` may only partially fill the buffer, but this code will look for 1s in the entire buffer.
(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.)
C# isn't the unholy incantations of unmaintainable cargo cult development. I don't care if its faster, I care if I have to weep for hours prior rewriting ungodly spaghetti. I've written code in many languages professionally, in the hierarchy of fuckery I've seen done in order to write code php is worse than perl, which is worse than js, which is worse than sucking on the teets of the chthonic elder gods. I would rather eat a hefty bowl of my own vomit while walking across a mile of broken glass before having to maintain anything written in php.
Ya, maybe. But it's easier to develop bug free code on C#. Speed has its advantages but it's not the only criteria used to decide what language to use.
I respectfully disagree. Modern PHP is an excellent language to work in, despite all the gripes old timers have from the old version 4/5 days. You can write top shelf Object oriented patterns, with first class dependency injection support. Type hinting. Built in Interface support. A really nice module/ import system. Composer is a fantastic dependency manger, and automatically gives you a magical class autoloader script with a small amount of configuration. Also PHP 8 includes all the modern conveniences you’d expect, like arrow function notation for closures, Enums, the Null Coalescing Operator, need I go on? :)
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.)
Isn’t this reading the file in 4K chunks in the php version and byte by byte in the C# version? If so they are completely different programs. The C# program would have 4096 times the number of method calls to Read()?
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!”
You can follow the link to the underlying C# library where it is reading it in 4kb chunks as well. PHP doesn’t buffer for you, so if you want to read a file one byte (to your peril) at a time, you can. It’ll be as fast as the async version of the C# implementation, but still slower than reading buffered.
1.that's not pretty good C# code. Though i can't comment on the quality of PHP
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
[+] [-] Mindless2112|4 years ago|reply
* 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.
[+] [-] cakoose|4 years ago|reply
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.
[+] [-] withinboredom|4 years ago|reply
To read in binary, you open with 'rb' and not just 'r'
[+] [-] deltaci|4 years ago|reply
[+] [-] cakoose|4 years ago|reply
(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.)
[+] [-] Someone|4 years ago|reply
My C# is very rusty (no pun intended) but I would guess the core of the program could be something like
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.[+] [-] stillbourne|4 years ago|reply
[+] [-] withinboredom|4 years ago|reply
[+] [-] overgard|4 years ago|reply
[+] [-] WheelsAtLarge|4 years ago|reply
[+] [-] 40four|4 years ago|reply
[+] [-] CharlesW|4 years ago|reply
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.)
[+] [-] alkonaut|4 years ago|reply
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).
[+] [-] notRobot|4 years ago|reply
> “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...
[+] [-] withinboredom|4 years ago|reply
[+] [-] goaty_tyaotg|4 years ago|reply
[+] [-] withinboredom|4 years ago|reply
In the latest addendum on the blog post, I ended up removing the counting of 1s.
[+] [-] Thristle|4 years ago|reply
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
[+] [-] behnamoh|4 years ago|reply
[+] [-] chaps|4 years ago|reply
[deleted]