Skip to content

Instantly share code, notes, and snippets.

@quicksketch
Created September 14, 2013 19:38
Show Gist options
  • Save quicksketch/6564942 to your computer and use it in GitHub Desktop.
Save quicksketch/6564942 to your computer and use it in GitHub Desktop.
YAML vs. JSON decoding against Drupal config files.
<?php
require_once __DIR__ . '/core/vendor/autoload.php';
require_once __DIR__ . '/core/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE);
use Symfony\Component\Yaml\Parser;
$parser = new Parser();
$yaml_files = file_scan_directory('./test_yaml', '/\.yml$/');
$start = microtime(TRUE);
$yaml_contents = array();
foreach ($yaml_files as $file) {
$parser->parse(file_get_contents($file->uri));
// Convert YAML to identical JSON copies of the same files.
//$json_version = json_encode($yaml_contents[$file->name]);
//$file_name = str_replace('.yml', '.json', str_replace('_yaml', '_json', $file->uri));
//file_put_contents($file_name, json_encode($yaml_contents[$file->name], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
}
$total = microtime(TRUE) - $start;
print "YAML parsing time: $total<br />";
$json_files = file_scan_directory('./test_json', '/\.json$/');
$start = microtime(TRUE);
$json_contents = array();
foreach ($json_files as $file) {
json_decode(file_get_contents($file->uri));
}
$total = microtime(TRUE) - $start;
print "JSON parsing time: $total<br />";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment