Last active
August 29, 2015 14:04
-
-
Save nyamsprod/1e759d801ff09834ed08 to your computer and use it in GitHub Desktop.
A simple benchmark to use League\Csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used the following script to benchmark the League\Csv insert performance. On my Ubuntu 13.10
It took me 10.7 seconds to process 100 000 rows.