Open jumperchen opened 7 years ago
I had a short look, but couldn't find anything obvious. The VM developers will hopefully have more information.
Btw. There is a Stopwatch
class that is designed for these kinds of measurements.
Below is the same example using a StringBuffer
. Normally it should be faster (or at least as fast), but currently it's ~50% slower.
Also interesting: replacing the j % 6
with the similar (and in theory faster) j.remainder(6)
actually made things much worse.
import 'dart:io';
// DartLang : Simulate a XML parsing.
main() {
var sw = new Stopwatch()..start();
String data = new File('large.xml')
.readAsStringSync();
print('File Length: ${data.length}');
int i = 0;
List<int> units = data.codeUnits;
var foo;
var buffer = new StringBuffer();
for (int j = 0; j < units.length; j++) {
buffer.writeCharCode(units[j]);
if (j % 6 == 5) {
i++;
foo = new Foo(buffer.toString());
buffer.clear();
}
}
print('Parsing time: ${sw.elapsedMilliseconds}ms');
print('Object creation times: ${i}');
}
class Foo {
String value;
Foo(this.value);
}
Scenario
We have a large zip file which contains a huge xml file to be parsed. In Java, it provides a standard unzip and xml parser class which is quite fast, and we want to do the same things in Dart VM, but Dartlang didn't provide such API for us to use.
After doing some testing, we figure out that the Dart String operation could be the problem.
Here is the simple example to demonstrate such issue (Note: Java's XML parser doesn't need to OP with String value, it could be more faster, see at the bottom info)
Note: StringBuffer in Dartlang is much slower than this codeUnits implementation.
The result
Parsing time: 572ms
Object creation times: 3395835
Parsign time: 938ms
Object creation times: 3395835
Parsing time: 4060ms
Object creation times: 27166668
Parsign time: 6223ms
Object creation times: 27166668
Parsing time: 7202ms
Object creation times: 54333335
Parsign time: 11871ms
Object creation times: 54333335
Extra Info
Here is the Java XML Parser example to parse the 326MB file.
It takes only 4658 ms to parse.
Note: The attached file for 326MB large.xml.zip