问题描述
在 python 和 c++ 中读取相同的羽毛文件时,在 python 中,函数 pyarrow.feather.read_table() 比我用于 C++ 的 API 表现得非常好。当我进一步调查时,我发现主要区别是因为在 python 中 read_table() API 使用了一个名为 memory_map 的标志(默认设置为 true)。当我禁用此标志时,c++ API 的性能比 Python 中的 read_table() 好。现在正如它所暗示的那样,C++ 默认不使用 memory_mapping,而是为了提高我想使用它的性能。请建议我使用 c++ 内存映射的方法,因为在 c++ 可用的文档中没有找到任何感兴趣的 API。 我使用的代码是 -
#!/usr/bin/env python3
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
import pyarrow.feather
import time
for i in range(20):
start_time = time.time()
table = pyarrow.feather.read_table('data'+str(i + 1)+'.feather')
end_time = time.time()
print("Time taken to read file is : ",(end_time - start_time)*1000,"ms")
C++ 代码是 -
void read_feather_to_table(std::string path,std::shared_ptr<arrow::Table> *feather_table){
std::shared_ptr <arrow::io::RandomAccessFile> input_file = file_system.OpenInputFile(path).ValueOrDie();
std::shared_ptr <arrow::ipc::feather::Reader> feather_reader = arrow::ipc::feather::Reader::Open(input_file).ValueOrDie();
auto t1 = std::chrono::high_resolution_clock::Now();
arrow::Status temp_status = feather_reader -> Read(feather_table);
auto t2 = std::chrono::high_resolution_clock::Now();
auto ms_int = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
std::cout << "Time taken to read file is : "<< ms_int.count()<< "ms\n";
return;
}
解决方法
使用 arrow::io::MemoryMappedFile
代替文件系统 API。 (您可以使用 arrow::io::MemoryMappedFile::Create
打开文件)目前没有打开内存映射文件的文件系统。 MemoryMappedFile
扩展了 arrow::io::InputStream
,因此您可以将其传递给 arrow::ipc::feather::Reader::Open
。