Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Challenge: Memory Optimization Audit | Performance Patterns
Python Memory Management
セクション 4.  5
single

single

Challenge: Memory Optimization Audit

メニューを表示するにはスワイプしてください

タスク

スワイプしてコーディングを開始

You are auditing a data model used in a financial reporting system. Your task is to apply the memory optimization techniques from this section to reduce the footprint of the Position class and verify the improvements with sys.getsizeof().

You are given the following starting class and dataset:

class Position:
    def __init__(self, position_id, ticker, quantity, price):
        self.position_id = position_id
        self.ticker = ticker
        self.quantity = quantity
        self.price = price

tickers = ["AAPL", "MSFT", "GOOG", "AMZN", "TSLA"]
  1. Import sys and copy.
  2. Define a class PositionOptimized with __slots__ = ("position_id", "ticker", "quantity", "price") and the same __init__ as Position.
  3. Create an instance of Position called pos_original with values (1, "AAPL", 100, 189.50).
  4. Create an instance of PositionOptimized called pos_optimized with the same values.
  5. Store sys.getsizeof(pos_original) in size_original and sys.getsizeof(pos_optimized) in size_optimized.
  6. Create a shallow copy of pos_original.__dict__ called budget_copy using copy.copy().
  7. Print size_original, size_optimized, and budget_copy.

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 4.  5
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt