Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kensnyder/e100bf89abc4f643ce98660eb1a78602 to your computer and use it in GitHub Desktop.
Save kensnyder/e100bf89abc4f643ce98660eb1a78602 to your computer and use it in GitHub Desktop.
Benchmarking exif_read_data()
<?php
$files = [
'1.2MB' => 'photo-1.2MB.jpg',
'2.5MB' => 'photo-2.5MB.jpg',
'5.6MB' => 'photo-5.6MB.jpg',
'7.9MB' => 'photo-7.9MB.jpg',
];
$handlers = [
'data uri base64_encode()' => function($binary) {
exif_read_data('data://image/jpeg;base64,' . base64_encode($binary));
},
'data uri urlencode()' => function($binary) {
exif_read_data('data://application/octet-stream,' . urlencode($binary));
},
'data uri rawurlencode()' => function($binary) {
exif_read_data('data://application/octet-stream,' . rawurlencode($binary));
},
'temp file' => function($binary) {
$path = '/tmp/photo.jpg';
file_put_contents($path, $binary);
exif_read_data($path);
unlink($path);
},
];
$iterations = 10;
echo "\nBenchmarking against $iterations iteration(s):\n";
foreach ($files as $size => $filename) {
$binary = file_get_contents($filename);
echo "\n--- $size jpg file ---\n";
foreach ($handlers as $name => $handler) {
$start = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$handler($binary);
}
$seconds = microtime(true) - $start;
echo number_format($seconds, 3) . "s - $name\n";
}
}
Benchmarking against 10 iteration(s):
--- 1.2MB jpg file ---
0.298s - data uri base64_encode()
0.701s - data uri urlencode()
0.702s - data uri rawurlencode()
0.045s - temp file
--- 2.5MB jpg file ---
0.597s - data uri base64_encode()
1.422s - data uri urlencode()
1.424s - data uri rawurlencode()
0.060s - temp file
--- 5.6MB jpg file ---
1.329s - data uri base64_encode()
3.311s - data uri urlencode()
3.320s - data uri rawurlencode()
0.120s - temp file
--- 7.9MB jpg file ---
1.907s - data uri base64_encode()
4.541s - data uri urlencode()
4.531s - data uri rawurlencode()
0.178s - temp file
Benchmarking against 10 iteration(s):
--- 1.2MB jpg file ---
0.625s - data uri base64_encode()
1.198s - data uri urlencode()
1.193s - data uri rawurlencode()
0.009s - temp file
--- 2.5MB jpg file ---
1.105s - data uri base64_encode()
2.454s - data uri urlencode()
2.428s - data uri rawurlencode()
0.017s - temp file
--- 5.6MB jpg file ---
2.660s - data uri base64_encode()
5.592s - data uri urlencode()
5.730s - data uri rawurlencode()
0.040s - temp file
--- 7.9MB jpg file ---
3.370s - data uri base64_encode()
8.020s - data uri urlencode()
8.072s - data uri rawurlencode()
0.061s - temp file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment