From 349c0de0912884388f2490d4bfd6905424ccf194 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Tue, 20 Mar 2012 20:45:27 +0100 Subject: Significantly speed up insertion into QJsonObject/Array The code was only allocating memory for the next insertion leading to a reallocation of the whole data for every single insertion. The code now reserves some space and uses a decent growth strategy to avoid repeated reallocs. Change-Id: I48b0feab71ba8ca73e7037f8460080f198b2f009 Reviewed-by: Jamey Hicks --- .../corelib/json/tst_bench_qtbinaryjson.cpp | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'tests/benchmarks/corelib') diff --git a/tests/benchmarks/corelib/json/tst_bench_qtbinaryjson.cpp b/tests/benchmarks/corelib/json/tst_bench_qtbinaryjson.cpp index e4e10ba2ba..c6956b4747 100644 --- a/tests/benchmarks/corelib/json/tst_bench_qtbinaryjson.cpp +++ b/tests/benchmarks/corelib/json/tst_bench_qtbinaryjson.cpp @@ -58,8 +58,12 @@ private Q_SLOTS: void parseNumbers(); void parseJson(); void parseJsonToVariant(); + void toByteArray(); void fromByteArray(); + + void jsonObjectInsert(); + void variantMapInsert(); }; BenchmarkQtBinaryJson::BenchmarkQtBinaryJson(QObject *parent) : QObject(parent) @@ -159,6 +163,30 @@ void BenchmarkQtBinaryJson::fromByteArray() } } +void BenchmarkQtBinaryJson::jsonObjectInsert() +{ + QJsonObject object; + QString test(QStringLiteral("testString")); + QJsonValue value(1.5); + + QBENCHMARK { + for (int i = 0; i < 1000; i++) + object.insert("testkey_" + i, value); + } +} + +void BenchmarkQtBinaryJson::variantMapInsert() +{ + QVariantMap object; + QString test(QStringLiteral("testString")); + QVariant variantValue(1.5); + + QBENCHMARK { + for (int i = 0; i < 1000; i++) + object.insert("testkey_" + i, variantValue); + } +} + QTEST_MAIN(BenchmarkQtBinaryJson) #include "tst_bench_qtbinaryjson.moc" -- cgit v1.2.3