summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/writer-benchmark.php30
-rwxr-xr-xexamples/xml.php15
2 files changed, 41 insertions, 4 deletions
diff --git a/examples/writer-benchmark.php b/examples/writer-benchmark.php
new file mode 100644
index 0000000..2b4e67b
--- /dev/null
+++ b/examples/writer-benchmark.php
@@ -0,0 +1,30 @@
+<?php
+
+//To work the benchmark need PHP5.5+ has it relies on PHP generator
+
+use League\Csv\Writer;
+
+require '../vendor/autoload.php';
+
+function generateRawData($start, $end)
+{
+ for ($i = $start; $i < $end; $i++) {
+ $index = $i;
+ yield [
+ 'cell--'.($index),
+ 'cell--'.($index+1),
+ 'cell--'.($index+2),
+ ];
+ }
+}
+
+$start = microtime(true);
+$nbrows = 200000;
+$csv = Writer::createFromPath('result.csv', 'w'); //to work make sure you have the write permission
+//$csv->setNullHandlingMode(Writer::NULL_HANDLING_DISABLED); //uncomment with useValidation to true to compare the speed
+$csv->useValidation(false); //change the 'true' to compare the value when validation are on
+$csv->insertAll(generateRawData(0, $nbrows));
+$duration = microtime(true) - $start;
+$memory = memory_get_peak_usage(true);
+echo 'adding '. $nbrows, ' rows in ', $duration, ' seconds using ', $memory, ' bytes', PHP_EOL;
+die(0);
diff --git a/examples/xml.php b/examples/xml.php
index 8db4c7c..7253adc 100755
--- a/examples/xml.php
+++ b/examples/xml.php
@@ -7,10 +7,17 @@ use League\Csv\Reader;
require '../vendor/autoload.php';
-$inputCsv = Reader::createFromPath('data/prenoms.csv');
-$inputCsv->setEncodingFrom('ISO-8859-15');
-$inputCsv->setDelimiter(';');
-$doc = $inputCsv->toXML('csv', 'ligne', 'cellule');
+$csv = Reader::createFromPath('data/prenoms.csv');
+$csv->setEncodingFrom('ISO-8859-15');
+$csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
+$csv->setDelimiter(';');
+//since version 7.0 only 10 rows will be converted using the query options
+$csv->setOffset(1);
+$csv->setLimit(10);
+$csv->addSortBy(function ($row1, $row2) {
+ return strcmp($row2[1], $row1[1]); //we order the result according to the number of firstname given
+});
+$doc = $csv->toXML('csv', 'ligne', 'cellule');
$xml = $doc->saveXML();
header('Content-Type: application/xml; charset="utf-8"');
header('Content-Length: '.strlen($xml));