Skip to content

Instantly share code, notes, and snippets.

@markstory
Created February 28, 2012 22:39
Show Gist options
  • Save markstory/1935774 to your computer and use it in GitHub Desktop.
Save markstory/1935774 to your computer and use it in GitHub Desktop.
hash benchmarks
public function test() {
$this->autoRender = false;
$data = array(
array(
'Article' => array(
'id' => '1',
'user_id' => '1',
'title' => 'First Article',
'body' => 'First Article Body'
),
'User' => array(
'id' => '1',
'user' => 'mariano',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
),
'Comment' => array(
array(
'id' => '1',
'article_id' => '1',
'user_id' => '2',
'comment' => 'First Comment for First Article',
),
array(
'id' => '2',
'article_id' => '1',
'user_id' => '4',
'comment' => 'Second Comment for First Article',
),
),
'Tag' => array(
array(
'id' => '1',
'tag' => 'tag1',
),
array(
'id' => '2',
'tag' => 'tag2',
)
),
'Deep' => array(
'Nesting' => array(
'test' => array(
1 => 'foo',
2 => array(
'and' => array('more' => 'stuff')
)
)
)
)
),
array(
'Article' => array(
'id' => '2',
'user_id' => '1',
'title' => 'Second Article',
'body' => 'Second Article Body',
'published' => 'Y',
),
'User' => array(
'id' => '2',
'user' => 'mariano',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
),
'Comment' => array(),
'Tag' => array()
),
array(
'Article' => array(
'id' => '3',
'user_id' => '1',
'title' => 'Third Article',
'body' => 'Third Article Body',
),
'User' => array(
'id' => '3',
'user' => 'mariano',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
),
'Comment' => array(),
'Tag' => array()
),
array(
'Article' => array(
'id' => '4',
'user_id' => '1',
'title' => 'Fourth Article',
'body' => 'Fourth Article Body',
),
'User' => array(
'id' => '4',
'user' => 'mariano',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
),
'Comment' => array(),
'Tag' => array()
),
array(
'Article' => array(
'id' => '5',
'user_id' => '1',
'title' => 'Fifth Article',
'body' => 'Fifth Article Body',
),
'User' => array(
'id' => '5',
'user' => 'mariano',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
),
'Comment' => array(),
'Tag' => array()
)
);
App::uses('Set', 'Utility');
App::uses('Hash', 'Utility');
$test = function () use ($data) {
Set::extract('/Article/title', $data);
};
$this->time($test, 'Set::extract() took ');
$test = function () use ($data) {
Hash::extract($data, '{n}.Article.title');
};
$this->time($test, 'Hash::extract() took ');
$test = function () use ($data) {
Set::countDim($data, true);
};
$this->time($test, 'Set::countDim() took ');
$test = function () use ($data) {
Hash::maxDimensions($data);
};
$this->time($test, 'Hash::maxDimensions() took ');
}
function time($callback, $message, $num = 1000) {
$start = microtime(true);
for ($i = 0; $i < $num; $i++) {
$callback();
}
$end = microtime(true);
debug($message . ($end - $start) . ' seconds');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment