0

I am trying to extend the zend partial helper to make it a little faster for my own needs. The code of how I do that is not important at this point.

I have created a unit test which acts weird and I can not understand the reason for it

this is the code:

    $garpTimeStart = microtime(true);
    for ($i = 0; $i < 999; $i++) {
        $this->_createOneGarpView();
    }
    $garpTimeTotal = microtime(true) - $garpTimeStart;

    $zendTimeStart = microtime(true);
    for ($i = 0; $i < 999; $i++) {
        $this->_createOneZendView();
    }
    $zendTimeTotal = microtime(true) - $zendTimeStart;

    $deltaPerformance = $zendTimeTotal - $garpTimeTotal;
    $this->assertTrue($deltaPerformance > 0);

ok, basically I create 1000 zendView objects and 1000 of my own garpView objects and I track the time for creating each of these.

Here comes the tricky part:

Simply by switching the order of the creation of the zend and garp views the test will fail. So, in other words, whichever I create first, will be created faster! (still, with a obvious difference in performance between the two)

So this in about 4 out of 5 times will fail

    $zendTimeStart = microtime(true);
    for ($i = 0; $i < 999; $i++) {
        $this->_createOneZendView();
    }
    $zendTimeTotal = microtime(true) - $zendTimeStart;

    $garpTimeStart = microtime(true);
    for ($i = 0; $i < 999; $i++) {
        $this->_createOneGarpView();
    }
    $garpTimeTotal = microtime(true) - $garpTimeStart;

    $deltaPerformance = $zendTimeTotal - $garpTimeTotal;
    $this->assertTrue($deltaPerformance > 0);

What am I missing? how can I create a more suitable environment to test this in?

iosifv
  • 1,153
  • 1
  • 10
  • 26
  • Do you have a measurable difference between your code and the zend code? Do you run other stuff on your system/database at the same time of the test? – Seb Jul 29 '14 at 14:48
  • of course, I don't run anything else. I thought that the differences in milliseconds are not important... – iosifv Jul 29 '14 at 15:09
  • It's hard to speculate without seeing the code of the functions you're benchmarking. Do they share any resources/classes which are setup on demand? – Tim Fountain Jul 29 '14 at 18:35
  • Differences in milliseconds are not important or might be due to other stuff on the computer running (or not running at that moment). And there's always other stuff running an a computer. So you could never be sure. If you run this 1000 times each to have a real increase in performance I would expect a few seconds difference between your "optimized" version and the zend version. If not, your version might not be measurably faster. – Seb Jul 30 '14 at 07:56
  • Thanks, I'll keep this in mind – iosifv Jul 30 '14 at 14:12
  • Having currently created 1000 objects in memory will decrease your system performance, so having the second 1000 objects created slower is a `normal` behaviour I think. Try `desctructing` the object after creation or try desctructing the first created 1000 objects before creating the second 1000 ones. Do this make sense? – php-dev Jul 30 '14 at 16:20

0 Answers0