Skip to content

Instantly share code, notes, and snippets.

@nyamsprod
Last active August 29, 2015 14:04
Show Gist options
  • Save nyamsprod/1e759d801ff09834ed08 to your computer and use it in GitHub Desktop.
Save nyamsprod/1e759d801ff09834ed08 to your computer and use it in GitHub Desktop.
A simple benchmark to use League\Csv
<?php
use League\Csv\Writer;
require 'vendor/autoload.php';
function generateRandomString($length = 10)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$str = '';
for ($i = 0; $i < $length; $i++) {
$str .= str_shuffle($characters)[0];
}
return $str;
}
/**
* Using PHP 5.5 generator to ease memory usage
*/
function generateRawData($start, $end)
{
for ($i = $start; $i < $end; $i++) {
/*yield [
generateRandomString(rand(5, 10)),
generateRandomString(rand(5, 10)),
generateRandomString(rand(5, 10)),
generateRandomString(rand(5, 10)),
generateRandomString(rand(5, 10)),
generateRandomString(rand(5, 10)),
generateRandomString(rand(5, 10)),
generateRandomString(rand(5, 10)),
generateRandomString(rand(5, 10)),
];*/
yield [
'JuKriljX',
'UJ2tpFl20E',
'JbsEKRYuvs',
'sN8XgYlPG',
'KtQcNZ2',
'LfaSslTtKk',
'59XtNhhJM',
'TzgPzFX09',
'xzBRbSHr'
]; // not using the generateRandomString() improve the time (<- this mimic more importing data from a DB)
}
}
$start = microtime(true);
$loop = $start;
$threshold = 10000;
$nbrows = 100000;
$csv = new Writer('result.csv', 'w'); //to work make sure you have the write permission
foreach (generateRawData(0, $nbrows) as $offset => $row) {
$csv->insertOne($row);
if ($offset > 0 && $offset % $threshold == 0) {
$new_loop = microtime(true);
$duration = $new_loop - $loop;
$loop = $new_loop;
echo $threshold,' rows added in ', $duration, ' seconds', PHP_EOL;
}
}
$duration = microtime(true) - $start;
echo $nbrows, ' added in ', $duration, ' seconds', PHP_EOL;
die(0);
@nyamsprod
Copy link
Author

I used the following script to benchmark the League\Csv insert performance. On my Ubuntu 13.10

PHP Version: PHP 5.5.3-1ubuntu2.6 (cli) (built: Jul  7 2014 16:54:53)
RAM : 2 GiO
Processor: Intel® Core™2 Duo CPU E7200 @ 2.53GHz × 2 

It took me 10.7 seconds to process 100 000 rows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment