-3

I want to performance test a couple of php functions. What is the easiest approach to repeat code 1000 times and return how long it took all together via microtime()?

Maybe a repeated loop? I came across a very simple code here on SO recently but forgot to favorite it.

Henrik Petterson
  • 6,862
  • 20
  • 71
  • 155

2 Answers2

2

What's so difficult about it? Let's modify this example from PHP Manual

<?php
$time_start = microtime(true);

$times=0;               // This couldn't be tough
while($times<1000)
{
   yourFunction();
   $times++;
}


$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Did yourFunction in $time seconds\n";
?> 
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
-1

You can use foreach and range() function

<?php

$start = microtime(true);

foreach(range(0, 1000) as $i) {
   yourFunction();
}

echo "Your function runs for " . (microtime(true) - $start) . " seconds";
OzzyCzech
  • 9,713
  • 3
  • 50
  • 34