我们知道io操作比内存操作慢,那到底慢多少,多大的数量级呢,或者写文件到底耗时间是多少呢?这决定我们能不能接收这样数量级别的耗时,
示例:写4K数据和拷贝4K数据
#include <iostream>
#include <chrono>
#include <fstream>
int main() {
// 测量内存操作的执行时间
int arry[1024] = {2};
int arry_tmp[1024] = {0};
std::ofstream outfile("data.bin", std::ios::binary | std::ios::out);
if (!outfile.is_open()) {
std::cerr << "Failed to open the file." << std::endl;
// return 1;
}
auto start_mem = std::chrono::steady_clock::now();
// 在这里执行内存操作
// 例如:循环访问一个大型数组,进行数据处理等
memcpy(arry_tmp, arry, sizeof(int)*1024);
auto end_mem = std::chrono::steady_clock::now();
auto mem_duration = std::chrono::duration_cast<std::chrono::microseconds>(end_mem - start_mem);
std::cout << "Memory operation took: " << mem_duration.count() << " microseconds" << std::endl;
// 测量IO操作的执行时间
auto start_io = std::chrono::steady_clock::now();
// 在这里执行IO操作
// 例如:读写文件,网络通信等
outfile.write(reinterpret_cast<const char*>(arry_tmp), sizeof(arry_tmp));
auto end_io = std::chrono::steady_clock::now();
auto io_duration = std::chrono::duration_cast<std::chrono::microseconds>(end_io - start_io);
std::cout << "IO operation took: " << io_duration.count() << " microseconds" << std::endl;
return 0;
}
输出
(py37) hq@nuc:~/java/my-project2$ ./a.out
Memory operation took: 0 microseconds
IO operation took: 28 microseconds
同步IO和异步IO耗时比较
#include <iostream>
#include <chrono>
#include <fstream>
#include <future>
#include <cstring>
void async_write_file(const std::string& filename, const int* data, size_t size) {
std::ofstream outfile(filename, std::ios::binary | std::ios::out);
if (!outfile.is_open()) {
std::cerr << "Failed to open the file." << std::endl;
return;
}
outfile.write(reinterpret_cast<const char*>(data), sizeof(int) * size);
outfile.close(); // 必须在异步任务中关闭文件,否则可能导致文件未完全写入
}
void sync_write_file(const std::string& filename, const int* data, size_t size) {
std::ofstream outfile(filename, std::ios::binary | std::ios::out);
if (!outfile.is_open()) {
std::cerr << "Failed to open the file." << std::endl;
return;
}
outfile.write(reinterpret_cast<const char*>(data), sizeof(int) * size);
outfile.close(); // 必须在异步任务中关闭文件,否则可能导致文件未完全写入
}
int main() {
int arry[1024] = {2};
int arry_tmp[1024] = {0};
auto start_mem = std::chrono::steady_clock::now();
std::memcpy(arry_tmp, arry, sizeof(int) * 1024);
auto end_mem = std::chrono::steady_clock::now();
auto mem_duration = std::chrono::duration_cast<std::chrono::microseconds>(end_mem - start_mem);
std::cout << "内存拷贝耗时: " << mem_duration.count() << " microseconds" << std::endl;
auto start_io = std::chrono::steady_clock::now();
std::future<void> io_result = std::async(std::launch::async, async_write_file, "data.bin", arry_tmp, 1024);
auto end_io = std::chrono::steady_clock::now();
auto io_duration = std::chrono::duration_cast<std::chrono::microseconds>(end_io - start_io);
std::cout << "异步io耗时: " << io_duration.count() << " microseconds" << std::endl;
start_io = std::chrono::steady_clock::now();
sync_write_file("data1.bin", arry_tmp, 1024);
end_io = std::chrono::steady_clock::now();
io_duration = std::chrono::duration_cast<std::chrono::microseconds>(end_io - start_io);
std::cout << "同步io耗时: " << io_duration.count() << " microseconds" << std::endl;
io_result.get(); // 等待异步任务完成
return 0;
}
输出
(py37) hq@nuc:~/java/my-project2$ ./a.out
内存拷贝耗时: 0 microseconds
异步io耗时: 61 microseconds
同步io耗时: 191 microseconds