0xDEADBEEF

RSS odkazy english edition

mem-test.php

18. 6. 2013
$testNumber = $argv[1];

const TEST_SIZE = 100000;

class TestPriv {
  private $a = 1;
  private $b = 2;
  private $c = 3;
}

class TestPub {
  public $a = 1;
  public $b = 2;
  public $c = 3;
}

class TestEmpty {}


$a = array();

// integer arrays

if ($testNumber == 1) {
  $test = "array_fill nulls";
  $a = array_fill(1, TEST_SIZE, null);
}

if ($testNumber == 2) {
  $test = "array_fill constant";
  $a = array_fill(1, TEST_SIZE, 1);
}

if ($testNumber == 3) {
  $test = "array_fill value";
  $val = 1;
  $a = array_fill(1, TEST_SIZE, $val);
}

if ($testNumber == 4) {
  $test = "range";
  $a = range(1, TEST_SIZE);
}

if ($testNumber == 5) {
  $test = "SplFixedArray empty";
  $a = new \SplFixedArray(TEST_SIZE);
}

if ($testNumber == 6) {
  $test = "SplFixedArray nulls";
  $a = new \SplFixedArray(TEST_SIZE);
  for ($i = 1; $i < TEST_SIZE; $i++) $a[$i] = null;
}

if ($testNumber == 7) {
  $test = "SplFixedArray const";
  $a = new \SplFixedArray(TEST_SIZE);
  for ($i = 1; $i < TEST_SIZE; $i++) $a[$i] = 1;
}


// classes, objects, arrays

if ($testNumber == 8) {
  $test = "private fields";
  for ($i = 0; $i < TEST_SIZE; $i++) {
    $a[] = new TestPriv;
  }
}


if ($testNumber == 9) {
  $test = "public fields default";
  for ($i = 0; $i < TEST_SIZE; $i++) {
    $a[] = new TestPub;
  }
}

if ($testNumber == 10) {
  $test = "public fields set";
  for ($i = 0; $i < TEST_SIZE; $i++) {
    $x = new TestPub;
    $x->a = 1;
    $x->b = 2;
    $x->c = 3;
    $a[] = $x;
  }
}

if ($testNumber == 11) {
  $test = "public fields set one";
  for ($i = 0; $i < TEST_SIZE; $i++) {
    $x = new TestPub;
    $x->a = 1;
    $a[] = $x;
  }
}

if ($testNumber == 12) {
  $test = "public fields add one";
  for ($i = 0; $i < TEST_SIZE; $i++) {
    $x = new TestPub;
    $x->x = 1;
    $a[] = $x;
  }
}

if ($testNumber == 13) {
  $test = "arrays constant keys";
  for ($i = 0; $i < TEST_SIZE; $i++) {
    $a[] = array(
      'a' => 1,
      'b' => 2,
      'c' => 3,
    );
  }
}

if ($testNumber == 14) {
  $test = "arrays variable keys";
  for ($i = 0; $i < TEST_SIZE; $i++) {
    $a[] = array(
      substr('abc', 0, 1) => 1,
      substr('abc', 1, 1) => 2,
      substr('abc', 2, 1) => 3,
    );
  }
}

if ($testNumber == 15) {
  $test = "objects";
  for ($i = 0; $i < TEST_SIZE; $i++) {
    $a[] = (object) array(
      'a' => 1,
      'b' => 2,
      'c' => 3,
    );
  }
}

if ($testNumber == 16) {
  $test = "empty class";
  for ($i = 0; $i < TEST_SIZE; $i++) {
    $x = new TestEmpty;
    $x->a = 1;
    $x->b = 2;
    $x->c = 3;
    $a[] = $x;
  }
}


$mem  = memory_get_usage(true);
$perElem = $mem / TEST_SIZE;
printf("%-25s mem: %9d, per element %.2f\n", $test, $mem, $perElem);

count($a);
píše k47 (@kaja47, k47)