I am testing file read/write performance in both Python and Dart, and I encountered a surprising result: Python is significantly faster than Dart for these operations. Here are the times I recorded:
Python:
- Write time: 10.28 seconds
- Read time: 4.88 seconds
- Total time: 15.16 seconds
Dart:
- Write time: 79 seconds
- Read time: 10 seconds
- Total time: 90 seconds
I expected Dart to perform similarly or even better than Python in this context, so I'm puzzled by these results. I’m running both tests on the same system (Mac) with similar code structures to ensure a fair comparison.
Questions:
- Are there any known reasons for this significant performance difference in file handling between Python and Dart?
- Could specific libraries, encoding formats, or other system-level factors in Python or Dart affect file I/O speed?
- Are there optimization techniques in Dart to improve file read/write performance?
I would appreciate any insights or suggestions on what might be causing this discrepancy and how to potentially optimize Dart's file I/O performance.
Here's the Python code:
def benchmark(cnt=200):
block_size = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" * (1024 * 1024)
file_path = "large_benchmark_test.txt"
start_time = time.time()
with open(file_path, "w") as file:
for _ in range(cnt):
file.write(block_size)
write_end_time = time.time()
with open(file_path, "r") as file:
while file.read(1024):
pass
read_end_time = time.time()
write_time = write_end_time - start_time
read_time = read_end_time - write_end_time
total_time = read_end_time - start_time
print(f"Python - Write: {write_time:.2f} s")
print(f"Python - Read: {read_time:.2f} s")
print(f"Python - Total: {total_time:.2f} s")
os.remove(file_path)
And the Dart code:
void benchmark({int cnt=200}) {
final blockSize = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' * 1024 * 1024;
final filePath = 'large_benchmark_test.txt';
final file = File(filePath);
final writeStartTime = DateTime.now();
final sink = file.openSync(mode: FileMode.write);
for (int i = 0; i < cnt; i++) {
sink.writeStringSync(blockSize);
}
sink.closeSync();
final writeEndTime = DateTime.now();
final writeTime = writeEndTime.difference(writeStartTime).inSeconds;
print("Dart (Synch) - Write: $writeTime s");
final readStartTime = DateTime.now();
final reader = file.openSync(mode: FileMode.read);
while (true) {
final buffer = reader.readSync(1024);
if (buffer.isEmpty) break;
}
reader.closeSync();
final readEndTime = DateTime.now();
final readTime = readEndTime.difference(readStartTime).inSeconds;
final totalTime = readEndTime.difference(writeStartTime).inSeconds;
print("Dart (Synch) - Read: $readTime s");
print("Dart (Synch) - Total: $totalTime s");
file.deleteSync();
}
benchmark(cnt: 10)? Are you observing a similar discrepancy?benchmark(cnt: 10)using the function I posted below? This would measure writing/reading 260MB of data. And perhaps mention the Python score for cnt 10 as well.