On 30/05/13 11:33, Tyler Romeo wrote:
Ran another test. I tested on the string '<&<&<&herllowodsiojgd<&sd<^<6&&"""' repeated 50 times, and I ran the replacement function 500,000 times. The results were:
htmlspecialchars with ENT_NOQUOTES: 14.025s htmlspecialchars without ENT_NOQUOTES: 13.457s strtr: 24.842s str_replace: 13.184s
Of course, these numbers tend to vary +/- 0.25s every time, so take it with a grain of salt.
With the PHP package used at Wikimedia (5.3.10-1ubuntu3.6+wmf1), with "taskset 1 nice -n-10", I get:
htmlspecialchars with ENT_NOQUOTES: 11.8s htmlspecialchars without ENT_NOQUOTES: 12.0s strtr: 24.8s str_replace: 12.9s
On 30/05/13 12:22, Daniel Friesen wrote:
- strtr is going to be faster in PHP 5.4 as they've changed the algorithm it uses
In 5.4, the strtr() hashtable implementation has been optimised, but it's still a hashtable. In htmlspecialchars(), the lookup table is just a plain C array:
static inline void find_entity_for_char_basic( unsigned int k, const entity_stage3_row *table, const unsigned char **entity, size_t *entity_len) { if (k >= 64U) { *entity = NULL; *entity_len = 0; return; }
*entity = table[k].data.ent.entity; *entity_len = table[k].data.ent.entity_len; }
It's hard to beat that.
-- Tim Starling