010011010110000101110100011101000010000001000010011101010111010001100011011010000110010101110010
Quick Links  
Home
Docs & How-Tos
Search
 
QueryPath

Books  

Drupal

Drupal, JavaScript, and jQuery.


LDAP


OpenCMS




 
Projects  
Anadem
Caryatid
OpenCms Modules
Pilaster
Sinciput
Utilities
Widgets (AJAX)
iPhone Apps
 

PHP: Strings vs. Character Arrays

Which is faster: Using strings, or using char arrays?

Author: M Butcher
Date: 2007-10-08 18:46:55 -0400

In PHP 5, is it faster to manipulate strings as strings, or as arrays? Is substr() faster than array_shift()? I found myself in a situation where I was carefully evaluating and modifying string values in PHP. Given my experience with Java, I wondered whether it would be faster to convert the strings to arrays, and manipulate them that way (using a standard parser-like method). So I devised a test. The conclusion? It is faster to treat PHP strings like... strings.

A common misnomer with PHP is that strings are arrays. While they can be treated like arrays, they are not arrays, and using array functions on them will (justifiably) return an error.

But what if you are doing a number of small conditional modifications to a string? Here's a very simple example: what if you are simply consuming a string? Is it faster to convert a string to an array of characters, and then use array manipulation functions, or is it faster to use clumsier string functions?

Here's the test I devised to find out. It is a simple comparison of using the substring() function vs. using an array_shift() function.

#!/usr/bin/php
<?php

$f = fopen( '/dev/urandom', 'r' );
$test_string = fread($f, 3 * 1024);
fclose( $f);


##
## Test array: shift
$j = strlen( $test_string);
$t = $test_string;

$t = str_split($t);
#print_r( $t ) && exit;

$start = microtime(true);
for( $i = 0; $i < $j; ++$i ) {
array_shift( $t );
}
$end = microtime(true);
$total = $end - $start;

$out = "It took $total seconds to process $j characters using shift.\n";
print $out;

unset( $start, $end);

##
## Test substring()
$j = strlen( $test_string);
$t = $test_string;

# Start timer...
$start = microtime(true);
for( $i = 0; $i < $j; ++$i ) {
$t = substr($t, 1);
}
$end = microtime(true);
# Timer stopped.
$total = $end - $start;

$out = "It took $total seconds to process $j characters using substring.\n";
print $out;
?
The script above first gets a chunk of random characters from the /dev/urandom device. It then tests two different strategies for consuming a string (Note: we don't do anything with the popped character -- we just throw it away).

In the first case, we consume the string by converting it to an array with str_split(), and then repeatedly shifting the array with array_shift().

I cheated.

I didn't include the amount of time it took the str_split() function to split the string.

Instead, I only counted the amount of time taken to shift the array.

On average, running the test on a 3072 character string, this method took about 0.175 seconds.

In the second case, I just continually called substr() on the string, chomping off one char at a time.

Now, from Java experience, I expected this to be rather time consuming. After all, isn't a string a more complex (== more memory, longer creation time) sort of beast than an array? Well, apparently this is not the case in PHP. The average amount of time it takes to run the same test of the same 3072 char string with substring() is only 0.005 seconds (or, to drag it out another significant digit, 0.0051 seconds).

So it turns out that treating strings like strings when performing these sorts of character-by-character tasks is still preferable to treating the string like an array.
News
R
S
S
News
Docs and How-To's
Releases
Links
 

Search

Google
  Web aleph-null.tv   

 

 

Questions? Comments? Consulting Opportunities? Email matt at aleph-null.tv.