我正在考虑在解析问题polars中使用 in 代替numpy,将结构化文本文件转换为字符表并在不同的列上进行操作。然而,这似乎比我执行的大多数操作polars慢大约 5 倍。numpy我想知道为什么会出现这种情况,以及考虑到应该polars更快,我是否做错了什么。
例子:
import requests
import numpy as np
import polars as pl
# Download the text file
text = requests.get("https://files.rcsb.org/download/3w32.pdb").text
# Turn it into a 2D array of characters
char_tab_np = np.array(file.splitlines()).view(dtype=(str,1)).reshape(-1, 80)
# Create a polars DataFrame from the numpy array
char_tab_pl = pl.DataFrame(char_tab_np)
# Sort by first column with numpy
char_tab_np[np.argsort(char_tab_np[:,0])]
# Sort by first column with polars
char_tab_pl.sort(by="column_0")
Run Code Online (Sandbox Code Playgroud)
使用%%timeitin 时Jupyter …