It would seem that some benchmarks are in order.
I have compiled several scripts for testing data storage in the form of a PHP array, both a CSV file and a MySQL DB, accessed through PDO.
Array test
Here are the scripts to test the array:
array inc.php:
$data = array( array( "key" => "something", "value" => "something else" ), array( "key" => "something", "value" => "something else" ), array( "key" => "something", "value" => "something else" ), array( "key" => "something", "value" => "something else" ), array( "key" => "something", "value" => "something else" ) );
array test.php
$start = microtime( TRUE ); for( $i = 0; $i < 10000; ++$i ) { include( "array-inc.php" ); $retrieve = $data[2]; } $finish = microtime( TRUE ); print $finish - $start;
CSV test
Here are the scripts for the CSV test:
values.csv:
key,value something,something else something,something else something,something else something,something else something,something else
csv.php:
$start = microtime( TRUE ); for( $i = 0; $i < 10000; ++$i ) { $fp = fopen( "values.csv", "r" ); $data = array(); while(( $line = fgetcsv( $fp )) !== FALSE ) { $data[] = $line; } $retrieve = $data[2]; } $finish = microtime( TRUE ); print $finish - $start;
MySQL test
And here is the script for the MySQL test (the table has an id, key and value column with the same five rows and values as above):
mysql.php:
$start = microtime( TRUE ); for( $i = 0; $i < 10000; ++$i ) { $query = "SELECT * FROM `values` WHERE id = :id"; $result = $pdo->prepare( $query ); $result->execute( array( ":id" => 2 )); $retrieve = $result->fetch( PDO::FETCH_ASSOC ); } $finish = microtime( TRUE ); print $finish - $start;
Each of them is configured to access one element from the stored data and cycle 100,000 times so that time can be more accurately measured. Here are the results:
I ran each of these tests three times and got the following values:
- Array:
- 1.050, 1.020, 1.114 seconds
- CSV:
- 1.264, 1.232, 1.105 seconds
- MySQL:
- 1.038, 1.149, 1.171 seconds
CSV seems to be a clear loser, but Array and MySQL are very close to the fact that Array may be a bit ahead of performance. Feel free to tweak the code above to provide results that are more relevant to your environment.