Sử dụng chương trình bên dưới Tôi thử kiểm tra tốc độ ghi trên đĩa bằng cách sử dụng std::ofstream
.Tại sao đĩa C++ của tôi viết thử nghiệm chậm hơn nhiều so với bản sao tệp đơn giản bằng bash?
Tôi đạt được khoảng 300 MiB/s khi viết 1 tệp GiB.
Tuy nhiên, một bản sao tệp đơn giản bằng cách sử dụng lệnh cp
nhanh hơn gấp hai lần.
Chương trình của tôi có đạt đến giới hạn phần cứng hoặc chương trình có thể nhanh hơn không?
#include <chrono>
#include <iostream>
#include <fstream>
char payload[1000 * 1000]; // 1 MB
void test(int MB)
{
// Configure buffer
char buffer[32 * 1000];
std::ofstream of("test.file");
of.rdbuf()->pubsetbuf(buffer, sizeof(buffer));
auto start_time = std::chrono::steady_clock::now();
// Write a total of 1 GB
for (auto i = 0; i != MB; ++i)
{
of.write(payload, sizeof(payload));
}
double elapsed_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - start_time).count();
double megabytes_per_ns = 1e3/elapsed_ns;
double megabytes_per_s = 1e9 * megabytes_per_ns;
std::cout << "Payload=" << MB << "MB Speed=" << megabytes_per_s << "MB/s" << std::endl;
}
int main()
{
for (auto i = 1; i <= 10; ++i)
{
test(i * 100);
}
}
Output:
Payload=100MB Speed=3792.06MB/s
Payload=200MB Speed=1790.41MB/s
Payload=300MB Speed=1204.66MB/s
Payload=400MB Speed=910.37MB/s
Payload=500MB Speed=722.704MB/s
Payload=600MB Speed=579.914MB/s
Payload=700MB Speed=499.281MB/s
Payload=800MB Speed=462.131MB/s
Payload=900MB Speed=411.414MB/s
Payload=1000MB Speed=364.613MB/s
Cập nhật
tôi đã thay đổi std::ofstream
-fwrite
:
#include <chrono>
#include <cstdio>
#include <iostream>
char payload[1024 * 1024]; // 1 MiB
void test(int number_of_megabytes)
{
FILE* file = fopen("test.file", "w");
auto start_time = std::chrono::steady_clock::now();
// Write a total of 1 GB
for (auto i = 0; i != number_of_megabytes; ++i)
{
fwrite(payload, 1, sizeof(payload), file);
}
fclose(file); // TODO: RAII
double elapsed_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - start_time).count();
double megabytes_per_ns = 1e3/elapsed_ns;
double megabytes_per_s = 1e9 * megabytes_per_ns;
std::cout << "Size=" << number_of_megabytes << "MiB Duration=" << long(0.5 + 100 * elapsed_ns/1e9)/100.0 << "s Speed=" << megabytes_per_s << "MiB/s" << std::endl;
}
int main()
{
test(256);
test(512);
test(1024);
test(1024);
}
nào cải thiện tốc độ để 668MiB/s cho một tập tin 1 GiB:
Size=256MiB Duration=0.4s Speed=2524.66MiB/s
Size=512MiB Duration=0.79s Speed=1262.41MiB/s
Size=1024MiB Duration=1.5s Speed=664.521MiB/s
Size=1024MiB Duration=1.5s Speed=668.85MiB/s
Đó là chỉ nhanh như dd
:
time dd if=/dev/zero of=test.file bs=1024 count=0 seek=1048576
real 0m1.539s
user 0m0.001s
sys 0m0.344s
Bạn đang thử nghiệm bản phát hành chương trình của bạn với tối ưu hóa? Bạn đã thử tăng kích thước bộ đệm chưa? –
Không nên là 'double megabyte_per_ns = MB/elapsed_ns; '? – zett42
Ngoài ra, bạn nên mở luồng ở chế độ nhị phân để so sánh nó với các phương pháp viết khác. Sử dụng 'std :: ofstream of (" test.file ", std :: ios :: binary)'. Tôi nhận được hiệu suất rất chặt chẽ giữa 'ofstream' và' fwrite' sau đó (sự khác biệt nằm trong phạm vi sai số đo lường). Trình biên dịch VC++ 2017. – zett42