Two examples and a question.
PHP example:
<?php $table = array(); $info = array(); for ($i = 0; $i < 65535; $i++) $table[$i] = ''; for ($i = 65535; $i < 65538; $i++) { $t = microtime(true); $table[$i] = ''; $t = microtime(true) - $t; $info[] = array($i, $t); } for ($i = 65538; $i < 131071; $i++) $table[$i] = ''; for ($i = 131071; $i < 131074; $i++) { $t = microtime(true); $table[$i] = ''; $t = microtime(true) - $t; $info[] = array($i, $t); } ?><pre><?php print_r($info) ?></pre> Result:
Array ( [0] => Array ( [0] => 65535 [1] => 5.0067901611328E-6 ) [1] => Array ( [0] => 65536 [1] => 0.0043141841888428 ) [2] => Array ( [0] => 65537 [1] => 9.5367431640625E-7 ) [3] => Array ( [0] => 131071 [1] => 3.0994415283203E-6 ) [4] => Array ( [0] => 131072 [1] => 0.0083508491516113 ) [5] => Array ( [0] => 131073 [1] => 9.5367431640625E-7 ) ) Python example:
from time import time from pprint import pprint table = {} info = [] for i in range(0, 21844): table[i] = '' for i in range(21844, 21847): t = time() table[i] = '' t = time() - t info.append((i, t)) for i in range(21847, 87380): table[i] = '' for i in range(87380, 87383): t = time() table[i] = '' t = time() - t info.append((i, t)) pprint(info) Result:
[(21844, 9.5367431640625e-07), (21845, 0.0022368431091308594), (21846, 9.5367431640625e-07), (87380, 2.1457672119140625e-06), (87381, 0.0048618316650390625), (87382, 1.9073486328125e-06)] It seems that, as I understand it, the complexity of inserting an element, taking into account the re-formation of the table, is still O (1), but if you somehow voice to the interpreter in advance what maximum number of keys in the table I expect, you can significantly reduce the constant before O, as well as avoid many memory reservations.
As I understand it, PHP and Python "out of the box" do not know how.
The question is: what scripting languages can this do? Exceptions: C ++ and Java answers will work, as well as an explanation of why this is not necessary at all.