[ create a new paste ] login | about

Link: http://codepad.org/aeNJ2u3O    [ raw code | output | fork ]

PHP, pasted on Jul 5:
<?php

error_reporting(E_ALL);
ini_set('display_errors', true);

function numeric_test() {
   $array = range(0, 100000);

   $start = microtime(true);

   for ($i = 0; $i < 100000; $i++) {
       $array[12345];
   }

   echo 'Lookup by numeric index:  ', microtime(true) - $start, PHP_EOL;
}

function numeric_string_test() {
   $array = range(0, 100000);

   $start = microtime(true);

   for ($i = 0; $i < 100000; $i++) {
       $array['12345'];
   }

   echo 'Lookup by numeric string: ', microtime(true) - $start, PHP_EOL;
}

function string_test() {
   $array = array();
   $key = 'a';
   for ($i = 0; $i < 100000; $i++) {
       $array[$key++] = 'foo';
   }

   $start = microtime(true);

   for ($i = 0; $i < 100000; $i++) {
       $array['abc'];
   }

   echo 'Lookup by string index:   ', microtime(true) - $start, PHP_EOL;
}


numeric_test();
numeric_string_test();
string_test();
numeric_test();
numeric_string_test();
string_test();
numeric_test();
numeric_string_test();
string_test();


Output:
1
2
3
4
5
6
7
8
9
Lookup by numeric index:  0.012346982955933
Lookup by numeric string: 0.07785701751709
Lookup by string index:   0.016586065292358
Lookup by numeric index:  0.011636018753052
Lookup by numeric string: 0.017691850662231
Lookup by string index:   0.076545000076294
Lookup by numeric index:  0.011714935302734
Lookup by numeric string: 0.017596960067749
Lookup by string index:   0.01695704460144


Create a new paste based on this one


Comments: