Simple Machines Blogs > Developers' Blog
PHP String Replacement Speed Comparison
Thantos:
Almost the same. Your strtr time weren't as bad but still the worse of them all. IIRC you only tested the first version of str_replace. Our tests had the same relationship between the functions.
Joshua Dickerson:
--- Quote from: groundup on June 03, 2007, 11:47:58 PM ---I remember doing a test a while back and receiving very different results. strtr() was the fastest out of the group. I might try your script out a little later and post my results.
I think it is important to note the PHP version, the webserver & version, the OS, the hardware, the load, and some specs relating to the software you are running. Since this is a benchmark of a core PHP function, I think the minimum that should be supplied is PHP version and the load.
Neverminding that stuff, nice to see some more benchmarks :). I loves me some benchmarks.
--- End quote ---
Was I drunk when I posted this? I must have been retarded. Oh well, stupid me.
KGIII:
I couldn't resist. Here are the results in case anyone should care.
sprintf: 1.38384985924 seconds
preg_replace: 3.27423620224 seconds
strtr: 40.9357118607 seconds
str_replace #1: 1.49082899094 seconds
str_replace #2: 1.94922995567 seconds
Daniel15:
Heh, I tried this on my slow little test server:
--- Quote ---sprintf: 2.92035603523 seconds
preg_replace: 5.3017771244 seconds
strtr: 145.762818813 seconds
str_replace #1: 7.66827106476 seconds
str_replace #2: 8.1126139164 seconds
--- End quote ---
--- Quote --- think it is important to note the PHP version, the webserver & version, the OS, the hardware, the load, and some specs relating to the software you are running.
--- End quote ---
Mine was on a Pentium III 650 MHz, Debian Testing, run via CLI.
If you're interested:
--- Quote ---
daniel@server:/tmp$ uname -a
Linux server.daniel15.com 2.6.18-4-vserver-686 #1 SMP Mon Mar 26 19:55:22 UTC 2007 i686 GNU/Linux
daniel@server:/tmp$ php -v
PHP 5.2.0-10+lenny1 (cli) (built: May 26 2007 08:23:53)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies
with XCache v1.2.0, Copyright (c) 2005-2006, by mOo
daniel@server:/tmp$ uptime
22:41:54 up 17 days, 4:04, 1 user, load average: 0.01, 0.02, 0.00
daniel@server:/tmp$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 8
model name : Pentium III (Coppermine)
stepping : 3
cpu MHz : 651.504
cache size : 256 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 2
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 sep mtrr pge mca cmov pat pse36 mmx fxsr sse up
bogomips : 1304.01
daniel@server:/tmp$ cat /proc/meminfo
MemTotal: 516788 kB
MemFree: 15272 kB
Buffers: 78256 kB
Cached: 151132 kB
SwapCached: 0 kB
Active: 342692 kB
Inactive: 101892 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 516788 kB
LowFree: 15272 kB
SwapTotal: 979956 kB
SwapFree: 979816 kB
Dirty: 244 kB
Writeback: 0 kB
AnonPages: 215192 kB
Mapped: 47692 kB
Slab: 49392 kB
PageTables: 2572 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
CommitLimit: 1238348 kB
Committed_AS: 518804 kB
VmallocTotal: 507896 kB
VmallocUsed: 3764 kB
VmallocChunk: 504084 kB
--- Code: ---server:/tmp# vps aux
USER PID CONTEXT %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0 MAIN 0.0 0.1 1944 640 ? Ss May23 0:02 init [2]
root 2 0 MAIN 0.0 0.0 0 0 ? S May23 0:00 [migration/0]
root 3 0 MAIN 0.0 0.0 0 0 ? SN May23 0:00 [ksoftirqd/0]
root 4 0 MAIN 0.0 0.0 0 0 ? S< May23 0:00 [events/0]
root 5 0 MAIN 0.0 0.0 0 0 ? S< May23 0:00 [khelper]
root 6 0 MAIN 0.0 0.0 0 0 ? S< May23 0:00 [kthread]
root 9 0 MAIN 0.0 0.0 0 0 ? S< May23 0:00 [kblockd/0]
root 10 0 MAIN 0.0 0.0 0 0 ? S< May23 0:00 [kacpid]
root 60 0 MAIN 0.0 0.0 0 0 ? S< May23 0:00 [kseriod]
root 95 0 MAIN 0.0 0.0 0 0 ? S May23 0:02 [pdflush]
root 96 0 MAIN 0.0 0.0 0 0 ? S< May23 0:22 [kswapd0]
root 97 0 MAIN 0.0 0.0 0 0 ? S< May23 0:00 [aio/0]
root 604 0 MAIN 0.0 0.0 0 0 ? S< May23 0:00 [khubd]
root 837 0 MAIN 0.0 0.0 0 0 ? S< May23 0:02 [kjournald]
root 999 0 MAIN 0.0 0.1 2176 604 ? S<s May23 0:00 udevd --daemon
root 1307 0 MAIN 0.0 0.0 0 0 ? S< May23 0:00 [kpsmoused]
root 1315 0 MAIN 0.0 0.0 0 0 ? S< May23 0:00 [kgameportd]
root 1513 0 MAIN 0.0 0.0 0 0 ? S< May23 0:00 [kmirrord]
root 1544 0 MAIN 0.0 0.0 0 0 ? S< May23 0:00 [kjournald]
root 1546 0 MAIN 0.0 0.0 0 0 ? S< May23 0:00 [kjournald]
root 1548 0 MAIN 0.0 0.0 0 0 ? S< May23 0:07 [kjournald]
root 1550 0 MAIN 0.0 0.0 0 0 ? S< May23 1:00 [kjournald]
root 4455 0 MAIN 0.0 0.1 1624 568 ? Ss May23 0:05 /sbin/syslogd
root 4471 0 MAIN 0.0 0.0 1576 388 ? Ss May23 0:00 /sbin/klogd -x
bind 4487 0 MAIN 0.0 0.7 31068 3784 ? Ssl May23 0:20 /usr/sbin/named -u bind
root 4543 0 MAIN 0.0 0.2 2664 1332 ? S May23 0:00 /bin/sh /usr/bin/mysqld_safe
mysql 4582 0 MAIN 0.0 5.4 127688 28420 ? Sl May23 2:34 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
root 4584 0 MAIN 0.0 0.0 1560 508 ? S May23 0:00 logger -p daemon.err -t mysqld_safe -i -t mysqld
root 4707 0 MAIN 0.0 0.1 1576 568 ? Ss May23 0:00 /usr/sbin/acpid -c /etc/acpi/events -s /var/run/acpid.socket
approx 4715 0 MAIN 0.0 0.2 3696 1460 ? S May23 0:00 /usr/sbin/approx
102 4770 0 MAIN 0.0 0.1 5272 988 ? Ss May23 0:00 /usr/sbin/exim4 -bd -q30m
root 4813 0 MAIN 0.0 0.1 1712 572 ? S May23 0:00 /usr/sbin/hddtemp -d -l 127.0.0.1 -p 7634 -s | /dev/hda
root 4842 0 MAIN 0.0 0.2 4932 1080 ? Ss May23 0:00 /usr/sbin/sshd
root 4845 0 MAIN 0.0 0.0 1764 396 ? Ss May23 0:01 /usr/sbin/ulogd -d
root 5112 49153 gforge01 0.0 0.1 1632 624 ? Ss May23 0:03 /sbin/syslogd
root 5113 49152 gforge01-web 0.0 0.1 1628 564 ? Ss May23 0:01 /sbin/syslogd
daemon 5129 49152 gforge01-web 0.0 0.0 1688 352 ? Ss May23 0:00 /sbin/portmap
root 5157 49152 gforge01-web 0.0 0.1 2200 884 ? Ss May23 0:00 /usr/sbin/cron
mysql 5168 49153 gforge01 0.0 0.7 19356 3688 ? S May23 0:16 /usr/lib/postgresql/8.1/bin/postmaster -D /var/lib/postgresql/8.1/main -c config_file=/etc/postgresql/8.1/main/postgresql.conf
mysql 5200 49153 gforge01 0.0 1.9 19488 9880 ? S May23 0:00 postgres: writer process
mysql 5201 49153 gforge01 0.0 0.3 10136 1988 ? S May23 0:44 postgres: stats buffer process
mysql 5202 49153 gforge01 0.0 0.2 9308 1264 ? S May23 0:26 postgres: stats collector process
sshd 5259 49153 gforge01 0.0 0.1 5308 996 ? Ss May23 0:00 /usr/sbin/exim4 -bd -q30m
list 5268 49153 gforge01 0.0 0.8 8708 4532 ? Ss May23 0:00 /usr/bin/python /usr/lib/mailman/bin/mailmanctl -s -q start
root 5278 49153 gforge01 0.0 0.2 4932 1112 ? Ss May23 0:00 /usr/sbin/sshd
list 5279 49153 gforge01 0.0 1.2 9216 6496 ? S May23 0:01 /usr/bin/python /var/lib/mailman/bin/qrunner --runner=ArchRunner:0:1 -s
list 5280 49153 gforge01 0.0 1.1 8344 5704 ? S May23 0:00 /usr/bin/python /var/lib/mailman/bin/qrunner --runner=BounceRunner:0:1 -s
list 5281 49153 gforge01 0.0 1.1 8372 5708 ? S May23 0:00 /usr/bin/python /var/lib/mailman/bin/qrunner --runner=CommandRunner:0:1 -s
list 5282 49153 gforge01 0.0 1.1 8688 6004 ? S May23 0:00 /usr/bin/python /var/lib/mailman/bin/qrunner --runner=IncomingRunner:0:1 -s
list 5283 49153 gforge01 0.0 1.1 8612 5740 ? S May23 0:00 /usr/bin/python /var/lib/mailman/bin/qrunner --runner=NewsRunner:0:1 -s
list 5284 49153 gforge01 0.0 1.1 8596 5856 ? S May23 0:00 /usr/bin/python /var/lib/mailman/bin/qrunner --runner=OutgoingRunner:0:1 -s
list 5285 49153 gforge01 0.0 1.1 8364 5700 ? S May23 0:00 /usr/bin/python /var/lib/mailman/bin/qrunner --runner=VirginRunner:0:1 -s
list 5286 49153 gforge01 0.0 1.1 8372 5700 ? S May23 0:00 /usr/bin/python /var/lib/mailman/bin/qrunner --runner=RetryRunner:0:1 -s
root 5319 49153 gforge01 0.0 0.1 2196 892 ? Ss May23 0:00 /usr/sbin/cron
root 5401 0 MAIN 0.0 0.1 4636 680 ? Ss May23 0:00 /usr/sbin/squid -D -sYC
proxy 5411 0 MAIN 0.0 3.3 19452 17492 ? S May23 6:26 (squid) -D -sYC
root 5413 0 MAIN 0.0 0.1 2192 884 ? Ss May23 0:00 /usr/sbin/cron
proxy 5422 0 MAIN 0.0 0.0 1420 288 ? Ss May23 0:01 (unlinkd)
root 5488 0 MAIN 0.0 1.1 9884 6088 ? Ss May23 0:00 /usr/bin/perl /usr/share/webmin/miniserv.pl /etc/webmin/miniserv.conf
root 5500 0 MAIN 0.0 0.0 1576 468 tty1 Ss+ May23 0:00 /sbin/getty 38400 tty1
root 5501 0 MAIN 0.0 0.0 1572 464 tty2 Ss+ May23 0:00 /sbin/getty 38400 tty2
root 5502 0 MAIN 0.0 0.0 1572 464 tty3 Ss+ May23 0:00 /sbin/getty 38400 tty3
root 5503 0 MAIN 0.0 0.0 1576 468 tty4 Ss+ May23 0:00 /sbin/getty 38400 tty4
root 5504 0 MAIN 0.0 0.0 1572 464 tty5 Ss+ May23 0:00 /sbin/getty 38400 tty5
root 5507 0 MAIN 0.0 0.0 1576 464 tty6 Ss+ May23 0:00 /sbin/getty 38400 tty6
root 5642 49154 public01 0.0 0.1 1632 624 ? Ss May23 0:00 /sbin/syslogd
root 5668 49154 public01 0.0 0.1 2200 884 ? Ss May23 0:00 /usr/sbin/cron
root 5727 0 MAIN 0.0 0.2 3956 1096 ? Ss May23 0:00 SCREEN
root 5728 0 MAIN 0.0 0.3 3964 1664 pts/1 Ss May23 0:00 /bin/bash
root 5729 0 MAIN 0.0 0.3 3940 1560 pts/1 S+ May23 0:00 /bin/bash /usr/sbin/chbind --silent --secure --ip 10.1.1.7/12 /usr/lib/util-vserver/exec-ulimit /etc/vservers/public01/ulimits /usr/sbin/vnamespace --enter 49154 -- /usr/sbin/vcontext --silent --migrate --chroot --xid 49154 --uid 0 --vlogin -- /bin/bash -login
root 5752 49154 public01 0.0 0.0 120 40 pts/1 S+ May23 0:00 login
root 5753 49154 public01 0.0 0.3 4000 1692 pts/2 Ss May23 0:00 /bin/bash -login
daniel 5758 49154 public01 0.0 0.2 3720 1064 pts/2 S May23 0:00 su - daniel
daniel 5759 49154 public01 0.0 0.5 3992 2624 pts/2 S May23 0:00 -su
daniel 5777 49154 public01 0.0 2.7 35132 14048 pts/2 S+ May23 20:42 /usr/bin/python /usr/bin/supybot FrihBot.conf
root 18770 0 MAIN 0.0 0.0 0 0 ? S May27 0:05 [pdflush]
root 12530 0 MAIN 0.0 0.1 2752 888 ? Ss Jun01 0:05 /usr/sbin/pppd call dsl-provider
root 13570 0 MAIN 0.0 0.2 6888 1440 ? Ss Jun01 0:00 /usr/sbin/nmbd -D
root 13572 0 MAIN 0.0 0.5 10068 2652 ? Ss Jun01 0:00 /usr/sbin/smbd -D
root 13577 0 MAIN 0.0 0.2 10068 1044 ? S Jun01 0:00 /usr/sbin/smbd -D
root 27739 49153 gforge01 0.0 1.4 19892 7444 ? Ss Jun03 0:01 /usr/sbin/apache2 -k start
root 28507 49152 gforge01-web 0.0 0.5 9568 2608 ? Ss Jun03 0:00 /usr/sbin/apache2 -k start
root 29574 0 MAIN 0.0 1.6 41436 8596 ? Ss Jun03 0:01 /usr/sbin/apache2 -k start
www-data 4919 49152 gforge01-web 0.0 0.8 11592 4560 ? S Jun07 0:12 /usr/sbin/apache2 -k start
www-data 6337 49152 gforge01-web 0.0 0.9 11872 4820 ? S Jun07 0:18 /usr/sbin/apache2 -k start
www-data 6488 49152 gforge01-web 0.0 1.0 12556 5500 ? S Jun07 0:17 /usr/sbin/apache2 -k start
www-data 28787 49152 gforge01-web 0.0 0.8 11460 4456 ? S 02:43 0:08 /usr/sbin/apache2 -k start
www-data 30873 49152 gforge01-web 0.0 0.8 11600 4532 ? S 05:27 0:08 /usr/sbin/apache2 -k start
www-data 30959 0 MAIN 0.0 2.1 42544 11192 ? S 05:28 0:03 /usr/sbin/apache2 -k start
www-data 30964 0 MAIN 0.0 2.0 43120 10540 ? S 05:28 0:02 /usr/sbin/apache2 -k start
www-data 32253 49152 gforge01-web 0.0 0.8 11600 4556 ? S 07:18 0:06 /usr/sbin/apache2 -k start
www-data 32256 49152 gforge01-web 0.0 0.6 10516 3484 ? S 07:18 0:02 /usr/sbin/apache2 -k start
www-data 32257 49152 gforge01-web 0.0 0.8 11464 4468 ? S 07:18 0:05 /usr/sbin/apache2 -k start
www-data 32266 49152 gforge01-web 0.0 0.6 10516 3484 ? S 07:18 0:02 /usr/sbin/apache2 -k start
www-data 32276 49152 gforge01-web 0.0 0.8 11464 4456 ? S 07:18 0:04 /usr/sbin/apache2 -k start
www-data 7150 0 MAIN 0.0 1.6 42632 8364 ? S 17:04 0:00 /usr/sbin/apache2 -k start
www-data 7175 0 MAIN 0.0 1.5 41764 8164 ? S 17:04 0:00 /usr/sbin/apache2 -k start
www-data 7207 0 MAIN 0.0 1.8 42352 9408 ? S 17:04 0:00 /usr/sbin/apache2 -k start
www-data 7482 49153 gforge01 0.2 2.4 27072 12844 ? S 17:11 0:45 /usr/sbin/apache2 -k start
mysql 7499 49153 gforge01 0.0 1.4 20836 7436 ? S 17:12 0:01 postgres: gforge gforge [local] idle
www-data 8064 0 MAIN 0.0 1.4 41836 7608 ? S 17:54 0:00 /usr/sbin/apache2 -k start
www-data 9227 49153 gforge01 0.1 2.4 26692 12516 ? S 19:31 0:22 /usr/sbin/apache2 -k start
mysql 9231 49153 gforge01 0.0 1.3 20696 6760 ? S 19:32 0:00 postgres: gforge gforge [local] idle
www-data 9376 0 MAIN 0.0 1.6 43060 8780 ? S 19:52 0:00 /usr/sbin/apache2 -k start
www-data 9758 49153 gforge01 0.1 2.3 26640 12368 ? S 20:32 0:11 /usr/sbin/apache2 -k start
mysql 9765 49153 gforge01 0.0 1.3 20932 6764 ? S 20:32 0:00 postgres: gforge gforge [local] idle
www-data 9920 0 MAIN 0.0 1.4 41832 7516 ? S 20:43 0:00 /usr/sbin/apache2 -k start
www-data 9921 49153 gforge01 0.1 2.3 26488 12232 ? S 20:43 0:13 /usr/sbin/apache2 -k start
www-data 9923 0 MAIN 0.0 1.4 41812 7552 ? S 20:43 0:00 /usr/sbin/apache2 -k start
www-data 9924 49153 gforge01 0.1 2.3 26252 12004 ? S 20:43 0:11 /usr/sbin/apache2 -k start
www-data 9928 0 MAIN 0.0 1.7 42220 8912 ? S 20:43 0:00 /usr/sbin/apache2 -k start
mysql 9935 49153 gforge01 0.0 1.2 20740 6352 ? S 20:43 0:00 postgres: gforge gforge [local] idle
mysql 9944 49153 gforge01 0.0 1.4 20868 7432 ? S 20:45 0:00 postgres: gforge gforge [local] idle
www-data 10412 49153 gforge01 0.1 2.4 26748 12468 ? S 21:33 0:07 /usr/sbin/apache2 -k start
mysql 10420 49153 gforge01 0.0 1.2 20724 6520 ? S 21:34 0:00 postgres: gforge gforge [local] idle
www-data 10546 49153 gforge01 0.1 2.4 26740 12420 ? S 21:47 0:05 /usr/sbin/apache2 -k start
mysql 10556 49153 gforge01 0.0 1.2 20724 6576 ? S 21:48 0:00 postgres: gforge gforge [local] idle
www-data 10619 49153 gforge01 0.1 2.3 26292 11944 ? S 22:01 0:04 /usr/sbin/apache2 -k start
mysql 10622 49153 gforge01 0.0 1.3 20740 6848 ? S 22:02 0:00 postgres: gforge gforge [local] idle
root 10999 0 MAIN 0.0 0.4 7704 2304 ? Ss 22:41 0:00 sshd: daniel [priv]
daniel 11001 0 MAIN 0.0 0.3 7860 1680 ? S 22:41 0:00 sshd: daniel@pts/0
daniel 11002 0 MAIN 0.0 0.6 5976 3500 pts/0 Ss 22:41 0:00 -bash
root 11046 0 MAIN 0.0 0.2 3712 1088 pts/0 S 22:42 0:00 su
root 11047 0 MAIN 0.0 0.3 3960 1672 pts/0 S 22:42 0:00 bash
www-data 11203 49154 public01 0.0 0.2 3924 1060 ? S 22:53 0:00 /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
www-data 11204 49154 public01 0.0 0.9 15040 4672 ? Ss 22:53 0:00 /usr/bin/php-cgi
www-data 11209 49154 public01 0.1 0.9 15040 4672 ? Ss 22:53 0:00 /usr/bin/php-cgi
www-data 11210 49154 public01 0.0 0.3 15040 1956 ? S 22:53 0:00 /usr/bin/php-cgi
www-data 11211 49154 public01 0.0 0.3 15040 1956 ? S 22:53 0:00 /usr/bin/php-cgi
www-data 11212 49154 public01 0.0 0.3 15040 1956 ? S 22:53 0:00 /usr/bin/php-cgi
www-data 11213 49154 public01 0.0 0.3 15040 1956 ? S 22:53 0:00 /usr/bin/php-cgi
www-data 11214 49154 public01 0.0 0.3 15040 1956 ? S 22:53 0:00 /usr/bin/php-cgi
www-data 11215 49154 public01 0.0 0.3 15040 1956 ? S 22:53 0:00 /usr/bin/php-cgi
www-data 11216 49154 public01 0.0 0.3 15040 1956 ? S 22:53 0:00 /usr/bin/php-cgi
www-data 11217 49154 public01 0.0 0.3 15040 1956 ? S 22:53 0:00 /usr/bin/php-cgi
root 11237 1 ALL_PROC 0.0 0.0 148 48 pts/0 S+ 22:54 0:00 vps aux
root 11238 1 ALL_PROC 0.0 0.1 3424 1000 pts/0 R+ 22:54 0:00 ps aux
--- End code ---
Edit: Added ps output (not that anybody cares :P)
--- End quote ---
Thantos:
If anyone else wants to post their test results feel free. I'll compile them together and post out some analysis of it.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version