[英]Why does my python notebook need excessive amounts of ram?
我讀取了一個 pgn 文件,提取了一些信息,然后將我的結果寫回了一個文件。 為什么 python 處理的 RAM 比我的變量加起來還要多? 示例:加載 10000 個棋類游戲后,python 需要 700mb 的 RAM,但列表只有 85kb 大。 200,000 場比賽打破了我的機器。
import chess.pgn
from tqdm import tqdm
def load_games(n_games: int) -> list[chess.pgn.Game]:
"""Load n games from the pgn file and return them as a list"""
with open("files\lichess_elite_2022-04.pgn") as pgn_file:
# Downloaded from: https://database.nikonoel.fr/
games = []
for i in tqdm(range(n_games), desc="Loading games", unit=" games"):
game = chess.pgn.read_game(pgn_file)
if game is not None:
games.append(game)
else:
break
return games
games = load_games(10000)
print(games.__sizeof__()/1000)
getsizeof 不給出列表元素的大小。 我通過使用 yield 語句來獲取生成器來解決
while True:
game = chess.pgn.read_game(pgn_file)
if game is not None:
yield game
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.