5 ms·
This is a pretty worthless comparison. * The C# version invokes EndOfStream up to 4096 times for every 1 call to feof in PHP version. * The C# version opens t
by Mindless2112 5y ago
This is a pretty worthless comparison.
* 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.
- withinboredom 5y agoThe C# version only makes a syscall if the buffer is empty: https://github.com/dotnet/runtime/blob/1ba0394d71a4ea6bee7f6b28a22d666b7b56f913/src/libraries/System.Private.CoreLib/src/System/IO/StreamReader.cs#L304 https://github.com/dotnet/runtime/blob/1ba0394d71a4ea6bee7f6... To read in binary, you open with 'rb' and not just 'r'
- Mindless2112 5y agoSyscalls are not the only consumer of wall clock time, which is what was measured. > 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#. [1] https://www.php.net/manual/en/function.fopen.php https://www.php.net/manual/en/function.fopen.php
- deltaci 5y agobut it makes function calls, it's not neglectable as it's called as many time as the bytes the file has
- withinboredom 5y agoThat’s the api provided, and part of the point. The other option is to read the buffer directly, but it isn’t exposed to the programmer.
- deltaci 5y agoYou just need to do File.OpenRead instead of OpenText to get a FileStream, then you can read the buffer
- h3h3 5y agothe overload is right below it int StreamReader.Read() int StreamReader.Read(char[] buffer, int index, int count) https://github.com/dotnet/runtime/blob/1ba0394d71a4ea6bee7f6b28a22d666b7b56f913/src/libraries/System.Private.CoreLib/src/System/IO/StreamReader.cs#L347 https://github.com/dotnet/runtime/blob/1ba0394d71a4ea6bee7f6...
- cakoose 5y agoThe previous post on this blog makes another worthless comparison, this time based on lines of code: https://withinboredom.info/blog/2022/03/16/my-favorite-language-has-changed-to-php/ 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/Algorithms/Sorters/External/ExternalMergeSorter.cs https://github.com/TheAlgorithms/C-Sharp/blob/master/Algorit... [2] https://www.w3resource.com/php-exercises/searching-and-sorting-algorithm/searching-and-sorting-algorithm-exercise-17.php 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-sorting-algorithm/searching-and-sorting-algorithm-exercise-7.php 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.