PHP: Compare time for summation and sorting across large runs

7 May

A little while back, I wrote a PHP script to generate a random set of large arrays and both summate them across one dimenion, and sort them.



The reason for this was to do a performance evaluation of how summation and sorting work as data sizes increase. Hope this is useful for someone! :)

<?php
// Some initial settings to make sure the program runs
// Make sure to set enough memory to hold all your objects
ini_set ('memory_limit', '960M');
error_reporting (E_ALL ^ E_NOTICE);

// Set your entries and runs here
$TOTAL_RUNS = 100;
$ENTRIES = 10000;

// Variables that we'll use
$arry = array();
$sum = array();

// Output the message
echo "For " . ($TOTAL_RUNS * $ENTRIES) . " records ($ENTRIES " .
	"entries over " . $TOTAL_RUNS . " runs):\n";

// Time taken to generate all elements
$start = microtime(true);
foreach (range(0,$TOTAL_RUNS) as $i) {
  foreach (range(0,$ENTRIES) as $j) {
    $arry[$i]["account$j"] = rand(0,100);
  }
}
echo "Generation = " . (microtime(true) - $start) . " secs\n";

// Time taken to aggregate elements across runs
$start = microtime(true);
foreach (range(0,$TOTAL_RUNS) as $i) {
  foreach (range(0,$ENTRIES) as $j) {
    $sum["account$j"] += (int)$arry[$i]["account$j"];
  }
}
echo "Summation = " . (microtime(true) - $start) . " secs\n";

// Time taken to sort aggregated elements
$start = microtime(true);
arsort($sum);
echo "Sorting = " . (microtime(true) - $start) . " secs\n";
?>
Advertisements

One Response to “PHP: Compare time for summation and sorting across large runs”

  1. itechonology May 11, 2013 at 12:21 am #

    Reblogged this on ITechonology.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: