Code: Select all
<?php
$str = '';
for ($i = 0; $i < 10000; $i++) {
$str .= 'A';
}
$start = microtime(true);
for ($i = 0; $i < 1000; $i++) {
$qp = '';
$bytes = unpack('C*', $str);
foreach ($bytes as $val) {
$qp .= sprintf('=%02X', $val);
}
}
echo '1000 of a 10KB string encodings took ' . (round(microtime(true) - $start, 4)) . ' seconds' . "\n";The thing I'm looking for optimization on is:
a) Splitting the string into an array of bytes (or any other way you can see to get all the byte values out)
b) Turning those byte values into the =N form.
IMPORTANT: The algorithm MUST deal with individual bytes at a time
As an example of just how slow this is, encoding that 10KB string of A's 1000 times on my Macbook takes 16 seconds. There's plenty of room for improvement if someone can pick better functions than unpack() and sprintf(). Maybe even just a different unpack() format will help ("C" is character data which is what I'm dealing with).