Last active
March 29, 2026 13:55
-
-
Save thekid/a1084bfd06336f390cd08d2fac015cc2 to your computer and use it in GitHub Desktop.
Base64 native functions vs. streaming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use io\streams\{FilterInputStream, FileInputStream, Streams}; | |
| use lang\Runtime; | |
| use util\cmd\Console; | |
| use util\profiling\Timer; | |
| $source= $argv[1]; | |
| $method= $argv[2]; | |
| $t= new Timer(); | |
| $t->start(); | |
| $data= match ($method) { | |
| 'native' => base64_decode(file_get_contents( | |
| '-' === $source ? 'php://stdin' : $source | |
| )), | |
| 'streams' => Streams::readAll(new FilterInputStream( | |
| '-' === $source ? Console::$in->stream() : new FileInputStream($source), | |
| 'convert.base64-decode' | |
| )), | |
| }; | |
| $t->stop(); | |
| Console::writeLinef( | |
| '%s => %d bytes (%.3f seconds, %.2f kB peak memory)', | |
| $source, | |
| strlen($data), | |
| $t->elapsedTime(), | |
| Runtime::getInstance()->peakMemoryUsage() / 1024 | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment