I am adding memoization to several functions. These functions accept 2-3 string parameters (object names), an optional int parameter (record identifier), and a logical parameter (including remote records). Each combination of parameters guarantees a unique result (so itβs worth caching).
I am wondering if it will be faster to concatenate the given parameters ( $param1 . $param2 . $param3etc.) and use this as an array key, or take the same concatenated string and use the md5 hash as the key. The string length of the concatenated parameters is from 20 to 32 characters in 99% of cases (on average about 27), while the md5 hash is always 32 characters.
Edit : md5 hash is only 16 bytes, not 32. Thanks Mjh.
I am inclined to the first option, since it:
- saves the cost of executing the md5 hash
- it usually stores a few bytes of memory (27 medium versus 32 hashes) (Mjh pointed out that this is not true: md5 is only 16 bytes) and
- since the md5 hash is just another line, it will usually be faster to compare a shorter line
The only reason I doubt it is because the vast majority of memoization functions seem to use hashes (md5), so I wonder if I am missing something.
Thanks in advance.
PS I forgot to mention: I separate the individual parameters with a symbol #that can never naturally occur in any of the parameters.
PPS No comments ankhzet is the best solution, because my line is almost unique to the beginning: crc32($paramString). Low memory and very fast checksum calculation function.
Crc32 () performance testing
script, 4 1 key => value. values 4 . keys , , 2 crc32() .
$test1Array = [];
$start1 = microtime(true);
for ($i = 0; $i < 1000000; $i++)
{
$test1Array[crc32("pagemanagement" . "#" . "staticblocktype" . "#" . $i . "#" . 1)] = "test " . $i;
}
$end1 = microtime(true);
$test2Array = [];
$start2 = microtime(true);
for ($j = 0; $j < 1000000; $j++)
{
$test2Array[crc32("pagemanagement" . "#" . "staticblocktype" . "#" . $i . "#" . 1)] = "test " . $j;
}
$end2 = microtime(true);
$test3Array = [];
$start3 = microtime(true);
for ($x = 0; $x < 1000000; $x++)
{
$test3Array["pagemanagement" . "#" . "staticblocktype" . "#" . $i . "#" . 1] = "test " . $x;
}
$end3 = microtime(true);
$test4Array = [];
$start4 = microtime(true);
for ($y = 0; $y < 1000000; $y++)
{
$test4Array["pagemanagement" . "#" . "staticblocktype" . "#" . $i . "#" . 1] = "test " . $y;
}
$end4 = microtime(true);
3 :
1: 3.9902291297913
2: 3.6312079429626
3: 0.91605305671692
4: 0.91405177116394
1: 3.9842278957367
2: 3.6172070503235
3: 0,91405200958252
4: 0.918053150177
1: 3.9842278957367
2: 3.6282079219818
3: 0.91205215454102
4: 0.91605186462402
"Test 2" "Test 4" ( " 1", , ), 3.6255409717560 "Test 2" 0.9160522619883 "Test 4", 2,7094887097677 (2.7094887097677/1000000) = 0,0000027094887 2,72 .
, , 4 crc32() , 27 . , 1 , 23 .
md5():
1: 4.2855787277221
2: 3,8108838399251
, md5() crc32(). , crc32() - 4 md5() 16.
: , 50-200 , , ~ 135 -540 ~ 1150-4600 .
- / , .