【发布时间】:2021-06-06 06:43:19
【问题描述】:
我已经编写了代码来使用 c++ 和 python 读取相同的 parquet 文件。 python 读取文件所需的时间比 c++ 中的要少得多,但是众所周知,c++ 中的执行速度比 python 中的要快。我在这里附上了代码-
#include <arrow/api.h>
#include <parquet/arrow/reader.h>
#include <arrow/filesystem/localfs.h>
#include <chrono>
#include <iostream>
int main(){
// ...
arrow::Status st;
arrow::MemoryPool* pool = arrow::default_memory_pool();
arrow::fs::LocalFileSystem file_system;
std::shared_ptr<arrow::io::RandomAccessFile> input = file_system.OpenInputFile("data.parquet").ValueOrDie();
// Open Parquet file reader
std::unique_ptr<parquet::arrow::FileReader> arrow_reader;
st = parquet::arrow::OpenFile(input, pool, &arrow_reader);
if (!st.ok()) {
// Handle error instantiating file reader...
}
// Read entire file as a single Arrow table
std::shared_ptr<arrow::Table> table;
auto t1 = std::chrono::high_resolution_clock::now();
st = arrow_reader->ReadTable(&table);
auto t2 = std::chrono::high_resolution_clock::now();
if (!st.ok()) {
// Handle error reading Parquet data...
}
else{
auto ms_int = std::chrono::duration_cast<std::chrono::milliseconds> (t2 - t1);
std::cout << "Time taken to read parquet file is : " << ms_int.count() << "ms\n";
}
}
我在python中使用的代码是-
#!/usr/bin/env python3
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
import time
start_time = time.time()
table = pq.read_table('data.parquet')
end_time = time.time()
print("Time taken to read parquet is : ",(end_time - start_time)*1000, "ms")
在为一个大小约为 87mb 的文件运行 c++ 代码时,c++ 的输出是 -
读取 parquet 文件所用时间:186ms
而对于 python,输出是 -
读取镶木地板的时间为:108.66141319274902 毫秒
为什么c++和python中函数read_table的执行时间会有这么大的差别?
【问题讨论】:
-
要得到毫秒,你需要乘以 1000,而不是乘以 100。
-
改变了,但python仍然更快,100毫秒与186毫秒相比,有什么可能的原因吗?
-
我的猜测是python阅读器(很可能也是用C或C++编写的)使用频率更高,因此得到了更好的优化。不过,我不确定 SO 是否可以在此问题上得到明确的答案,如果有必要了解,您可能需要联系较慢 api 的作者。
-
你运行过几次了吗?也许存在“缓存变暖”效应。
-
@SourabhKulhari -- 请提供您用于构建 C++ 程序的优化设置。任何涉及速度的 C++ 问题都不应遗漏这个非常 重要的信息。如果您正在运行未优化或“调试”构建,那么您显示的时间是没有意义的。您应该只进行时间优化(发布)构建。
标签: python c++ parquet pyarrow apache-arrow