Skip to content

Instantly share code, notes, and snippets.

@thekid
Last active March 29, 2026 13:55
Show Gist options
  • Select an option

  • Save thekid/a1084bfd06336f390cd08d2fac015cc2 to your computer and use it in GitHub Desktop.

Select an option

Save thekid/a1084bfd06336f390cd08d2fac015cc2 to your computer and use it in GitHub Desktop.
Base64 native functions vs. streaming
<?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