Skip to content

Instantly share code, notes, and snippets.

@quicksketch
Created September 16, 2013 00:09
Show Gist options
  • Save quicksketch/6575436 to your computer and use it in GitHub Desktop.
Save quicksketch/6575436 to your computer and use it in GitHub Desktop.
Symfony YAML vs. Spyc YAML vs. JSON vs. PHP parsing times.
<?php
require_once __DIR__ . '/core/vendor/autoload.php';
require_once __DIR__ . '/core/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE);
$yaml_files = file_scan_directory('./test_yaml', '/\.yml$/');
$start = microtime(TRUE);
$yaml_contents = array();
$parser = new Symfony\Component\Yaml\Parser();
foreach ($yaml_files as $file) {
$symfony_yaml[$file->name] = $parser->parse(file_get_contents($file->uri));
// Convert YAML to identical JSON copies of the same files.
//$json_version = json_encode($symfony_yaml[$file->name]);
//$file_name = str_replace('.yml', '.json', str_replace('_yaml', '_json', $file->uri));
//file_put_contents($file_name, json_encode($symfony_yaml[$file->name]));
// Convert YAML to identical PHP copies of the same files.
// $function_name = str_replace(array('.', '-'), '_', str_replace('.yml', '', $file->name));
// $php_version = '<?php
// function ' . $function_name . '_value() {
// return ' . var_export($spyc_yaml[$file->name], TRUE) . ';
// }';
// $file_name = str_replace('.yml', '.php', str_replace('_yaml', '_php', $file->uri));
// file_put_contents($file_name, $php_version);
}
$total = microtime(TRUE) - $start;
print "Symfony YAML parsing time: $total<br />";
// -----------------------------------------------------------------------------
$start = microtime(TRUE);
$yaml_contents = array();
require_once __DIR__ . '/spyc/Spyc.php';
foreach ($yaml_files as $file) {
$spyc_yaml[$file->name] = spyc_load_file($file->uri);
}
$total = microtime(TRUE) - $start;
print "Spyc YAML parsing time: $total<br />";
// foreach ($symfony_yaml as $filename => $result) {
// if ($spyc_yaml[$filename] != $result) {
// print "$filename is different<br />";
// print_r($result);
// print "<br />";
// print_r($spyc_yaml[$filename]);
// print "<br />";
// }
// }
// -----------------------------------------------------------------------------
$json_files = file_scan_directory('./test_json', '/\.json$/');
$start = microtime(TRUE);
$json_contents = array();
foreach ($json_files as $file) {
$json_results[$file->name] = json_decode(file_get_contents($file->uri), TRUE);
}
$total = microtime(TRUE) - $start;
print "JSON parsing time: $total<br />";
foreach ($symfony_yaml as $filename => $result) {
if ($json_results[$filename] != $result) {
print "$filename is different<br />";
print_r($result);
print "<br />";
print_r($json_results[$filename]);
print "<br />";
}
}
// -----------------------------------------------------------------------------
$php_files = file_scan_directory('./test_php', '/\.php$/');
$php_contents = array();
$start = microtime(TRUE);
foreach ($php_files as $file) {
include_once($file->uri);
$function_name = str_replace(array('.', '-'), '_', str_replace('.yml', '', $file->name)) . '_value';
$php_contents[$file->name] = $function_name();
}
$total = microtime(TRUE) - $start;
print "PHP include and parsing time: $total<br />";
$php_contents2 = array();
$start = microtime(TRUE);
foreach ($php_files as $file) {
$function_name = str_replace(array('.', '-'), '_', str_replace('.yml', '', $file->name)) . '_value';
$php_contents2[$file->name] = $function_name();
}
$total = microtime(TRUE) - $start;
print "PHP parsing-only time: $total<br />";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment