[ create a new paste ] login | about

Link: http://codepad.org/Xy4BqiTw    [ 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 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();
string_test();
numeric_test();
string_test();
numeric_test();
string_test();


Output:
1
2
3
4
5
6
Lookup by numeric index: 0.012243986129761
Lookup by string index:  0.016739845275879
Lookup by numeric index: 0.011622190475464
Lookup by string index:  0.016694068908691
Lookup by numeric index: 0.011900186538696
Lookup by string index:  0.016508102416992


Create a new paste based on this one


Comments: