Thanks again Bob,
I tried to get some hard numbers and it seems the way I choose is the worst =)...eg. $someobj->toArray() seems to be the slowest of all..., i tried to create some sort of benchmark but I don't know how reliable it really is, as before, this is run easiest in the console extra:
<?php
$query = array('parent' => 15);
$tStart = $modx->getMicroTime();
$pdoFetch = $modx->getService('pdofetch','pdoFetch',$modx->getOption('pdotools.core_path',null,$modx->getOption('core_path').'components/pdotools/').'model/pdotools/',array());
if (!($pdoFetch instanceof pdoFetch)) {return '';}
$tEnd = $modx->getMicroTime();
echo '<pre>Needed ' . sprintf('%2.4f s', $tEnd - $tStart) . ' to load pdoFetch service</pre>';
// set up the round
$rounds = 5;
// prepare the benchmark arrays
$timingarrays = array('pdofetch' => array(), 'xpdo0' => array(), 'xpdo1' => array(), 'xpdo2' => array(), 'xpdo3' => array(), 'xpdo4' => array(), 'xpdo5' => array(), 'xpdo6' => array());
// start the looping
for ($i=0;$i<$rounds;$i++) {
$tStart = $modx->getMicroTime();
$resarrs = $pdoFetch->getCollection('modResource', $query);
foreach ($resarrs as $resarr) {
$task = $resarr['pagetitle'];
}
$tEnd = $modx->getMicroTime();
$timingarrays['pdofetch'][] = $tEnd - $tStart;
// ------------------------------
$tStart = $modx->getMicroTime();
$resobjs = $modx->getCollection('modResource', $query);
foreach ($resobjs as $resobj) {
$task = $resobj->get('pagetitle');
}
$tEnd = $modx->getMicroTime();
$timingarrays['xpdo0'][] = $tEnd - $tStart;
// ------------------------------
$tStart = $modx->getMicroTime();
$resobjs = $modx->getCollection('modResource', $query);
foreach ($resobjs as $resobj) {
$resarr = $resobj->toArray();
$task = $resarr['pagetitle'];
}
$tEnd = $modx->getMicroTime();
$timingarrays['xpdo1'][] = $tEnd - $tStart;
// ------------------------------
$tStart = $modx->getMicroTime();
$resobjs = $modx->getCollection('modResource', $query);
foreach ($resobjs as $resobj) {
$resarr = $resobj->toArray('', true, false, false);
$task = $resarr['pagetitle'];
}
$tEnd = $modx->getMicroTime();
$timingarrays['xpdo2'][] = $tEnd - $tStart;
// ------------------------------
$tStart = $modx->getMicroTime();
$resobjs = $modx->getCollection('modResource', $query);
foreach ($resobjs as $resobj) {
$resarr = $resobj->toArray('', true, true, false);
$task = $resarr['pagetitle'];
}
$tEnd = $modx->getMicroTime();
$timingarrays['xpdo3'][] = $tEnd - $tStart;
// ------------------------------
$tStart = $modx->getMicroTime();
$resobjs = $modx->getCollection('modResource', $query);
foreach ($resobjs as $resobj) {
$resarr = $resobj->toArray('', true, true, true);
$task = $resarr['pagetitle'];
}
$tEnd = $modx->getMicroTime();
$timingarrays['xpdo4'][] = $tEnd - $tStart;
// ------------------------------
$tStart = $modx->getMicroTime();
$resobjs = $modx->getIterator('modResource', $query);
foreach ($resobjs as $resobj) {
$task = $resobj->get('pagetitle');
}
$tEnd = $modx->getMicroTime();
$timingarrays['xpdo5'][] = $tEnd - $tStart;
// ------------------------------
$tStart = $modx->getMicroTime();
$resobjs = $modx->getIterator('modResource', $query);
foreach ($resobjs as $resobj) {
$resarr = $resobj->toArray('', true, false, false);
$task = $resarr['pagetitle'];
}
$tEnd = $modx->getMicroTime();
$timingarrays['xpdo6'][] = $tEnd - $tStart;
}
// get the number of items that was looped trough
$tStart = $modx->getMicroTime();
$rescount = $modx->getCount('modResource', $query);
$tEnd = $modx->getMicroTime();
echo '<pre>Needed ' . sprintf('%2.4f s', $tEnd - $tStart) . ' to to do $modx->getCount()</pre>';
// calculate the avarage time spent
foreach ($timingarrays as $key => $timingarray) {
$average = average($timingarray);
switch($key) {
case 'pdofetch':
$test = '$pdoFetch->getCollection()';
break;
case 'xpdo0':
$test = '$modx->getCollection() with $obj->get()';
break;
case 'xpdo1':
$test = '$modx-getCollection() with $obj->toArray()';
break;
case 'xpdo2':
$test = '$modx-getCollection() with $obj->toArray("", true, false, false)';
break;
case 'xpdo3':
$test = '$modx-getCollection() with $obj->toArray("", true, true, false)';
break;
case 'xpdo4':
$test = '$modx-getCollection() with $obj->toArray("", true, true, true)';
break;
case 'xpdo5':
$test = '$modx-getIterator() with $obj->get()';
break;
case 'xpdo6':
$test = '$modx-getIterator() with $obj->toArray("", true, false, false)';
break;
default:
$test = 'undefined';
break;
}
echo '<pre>' . $test . ' needed ' . sprintf('%2.4fs', $average) . ' in avarage over ' . $rounds . ' rounds looping trough ' . $rescount . ' resources</pre>';
}
function average($arr) {
$count = count($arr); //total numbers in array
foreach ($arr as $value) {
$total = $total + $value; // total value of array numbers
}
$average = ($total/$count); // get average value
return $average;
}
in my case I got a result like this:
RUN1
Needed 0,0080 s to load pdoFetch service
Needed 0,0046 s to to do $modx->getCount()
$pdoFetch->getCollection() needed 0,0145s in avarage over 5 rounds looping trough 38 resources
$modx->getCollection() with $obj->get() needed 0,6185s in avarage over 5 rounds looping trough 38 resources
$modx-getCollection() with $obj->toArray() needed 0,5654s in avarage over 5 rounds looping trough 38 resources
$modx-getCollection() with $obj->toArray("", true, false, false) needed 0,4354s in avarage over 5 rounds looping trough 38 resources
$modx-getCollection() with $obj->toArray("", true, true, false) needed 0,4476s in avarage over 5 rounds looping trough 38 resources
$modx-getCollection() with $obj->toArray("", true, true, true) needed 0,4470s in avarage over 5 rounds looping trough 38 resources
$modx-getIterator() with $obj->get() needed 0,4265s in avarage over 5 rounds looping trough 38 resources
$modx-getIterator() with $obj->toArray("", true, false, false) needed 0,4439s in avarage over 5 rounds looping trough 38 resources
RUN2
Needed 0,0080 s to load pdoFetch service
Needed 0,0048 s to to do $modx->getCount()
$pdoFetch->getCollection() needed 0,0136s in avarage over 5 rounds looping trough 38 resources
$modx->getCollection() with $obj->get() needed 0,4608s in avarage over 5 rounds looping trough 38 resources
$modx-getCollection() with $obj->toArray() needed 0,6245s in avarage over 5 rounds looping trough 38 resources
$modx-getCollection() with $obj->toArray("", true, false, false) needed 0,4478s in avarage over 5 rounds looping trough 38 resources
$modx-getCollection() with $obj->toArray("", true, true, false) needed 0,4512s in avarage over 5 rounds looping trough 38 resources
$modx-getCollection() with $obj->toArray("", true, true, true) needed 0,4978s in avarage over 5 rounds looping trough 38 resources
$modx-getIterator() with $obj->get() needed 0,4510s in avarage over 5 rounds looping trough 38 resources
$modx-getIterator() with $obj->toArray("", true, false, false) needed 0,4546s in avarage over 5 rounds looping trough 38 resources
RUN3
Needed 0,0079 s to load pdoFetch service
Needed 0,0046 s to to do $modx->getCount()
$pdoFetch->getCollection() needed 0,0159s in avarage over 5 rounds looping trough 38 resources
$modx->getCollection() with $obj->get() needed 0,4639s in avarage over 5 rounds looping trough 38 resources
$modx-getCollection() with $obj->toArray() needed 0,6928s in avarage over 5 rounds looping trough 38 resources
$modx-getCollection() with $obj->toArray("", true, false, false) needed 0,4300s in avarage over 5 rounds looping trough 38 resources
$modx-getCollection() with $obj->toArray("", true, true, false) needed 0,3911s in avarage over 5 rounds looping trough 38 resources
$modx-getCollection() with $obj->toArray("", true, true, true) needed 0,4952s in avarage over 5 rounds looping trough 38 resources
$modx-getIterator() with $obj->get() needed 0,5236s in avarage over 5 rounds looping trough 38 resources
$modx-getIterator() with $obj->toArray("", true, false, false) needed 0,5480s in avarage over 5 rounds looping trough 38 resources
I know pdoTools is fast (and as you said, going over pure PDO is fastest, and I think this is what pdoTools does...converting xPDO queries as we know and love them to pure PDO...right?) but the test shows such extrem differences that I almost wish that pdoTools was actually part of the core...this is crazy^^...where does all that time get lost in the native methods?? any idea?
basically what my test says is "go with pdoTools whenever you can" and if not possible use $modx->getIterator() and from there access the fields with ->get() (actually I have to do further tests to see what happens if I access not only one but many fields with get('whatever')...could be that performance goes down when calling get many times, compared to toArray() I mean?)
[ed. note: exside last edited this post 12 years, 10 months ago.]