[Solar-talk] locale strings + replacements

Rodrigo Moraes rodrigo.moraes at gmail.com
Thu Nov 23 02:44:12 PST 2006


Hey,
It seems it's Thanksgiving in the USA, so happy Thanksgiving. :)

I was curious about vsprintf() performance and wanted to play with
Apache ab for a while, so I've benchmarked vsprintf() versus
str_replace(). In short words, vsprintf() wins but with a really
insignificant margin.

I'm using vsprintf() in getText helpers, but as Antti remembered it
doesn't allow replacements in validation messages and other internal
uses, and that's why Solar::locale() should implement something like
this.

Sorry for such a frivolous benchmark, but I was looking for some
benchmark fun and I hope it will be interesting to know that printf()
functions are really the best choice for replacements. :)

A note: using array() as parameters for str_replace slows down things
a bit, and it loses by a margin of 10% then.

Here's the summary:

vsprintf() script
-----------------------------------------------------
<?php
$format = 'The %2$s contains %1$d monkeys.';
$data = array(7, 'house');
echo vsprintf($format, $data);
?>

str_replace() script
-----------------------------------------------------
<?php
$format = 'The %2$s contains %1$d monkeys.';
$format = str_replace('%2$s', 'house', $format);
echo str_replace('%1$d', 7, $format);
?>

vsprintf() results
-----------------------------------------------------
Concurrency Level:      50
Time taken for tests:   60.000 seconds
Complete requests:      33222
Failed requests:        0
Write errors:           0
Total transferred:      7342283 bytes
HTML transferred:       963467 bytes
Requests per second:    553.70 [#/sec] (mean)
Time per request:       90.302 [ms] (mean)
Time per request:       1.806 [ms] (mean, across all concurrent requests)
Transfer rate:          119.50 [Kbytes/sec] received

str_replace() results
-----------------------------------------------------
Concurrency Level:      50
Time taken for tests:   60.000 seconds
Complete requests:      32910
Failed requests:        0
Write errors:           0
Total transferred:      7273331 bytes
HTML transferred:       954419 bytes
Requests per second:    548.50 [#/sec] (mean)
Time per request:       91.158 [ms] (mean)
Time per request:       1.823 [ms] (mean, across all concurrent requests)
Transfer rate:          118.37 [Kbytes/sec] received


More information about the solar-talk mailing list