9
10
記事投稿キャンペーン 「2024年!初アウトプットをしよう」

Python: 損をしないために知っておきたい実践的なテクニック

Last updated at Posted at 2024-01-15

概要

Pythonにおいて、知らないと損をする可能性のあるテクニックに焦点を当て、実践的なアプローチで解説します。
随時追加予定です。

マルチスレッドを使用してもパフォーマンスが改善されない場合がある

以下のようなファイルの読み込みがある場合、マルチスレッドを使用しても効果が期待できません。

import threading

def simple_calculation(iterations):
    result = 0
    for _ in range(iterations):
        result += 1
    return result

def perform_calculations():
    threads = []
    iterations_per_thread = 100000000 // 5  # 各スレッドが処理するイテレーション数
    for _ in range(5):
        thread = threading.Thread(target=simple_calculation, args=(iterations_per_thread,))
        thread.start()
        threads.append(thread)

    for thread in threads:
        thread.join()

def simple_calculation_common(iterations):
    result = 0
    for _ in range(iterations):
        result += 1
    return result

if __name__ == "__main__":
    import time

    # マルチスレッドを使用する場合
    start_time = time.time()
    perform_calculations()
    end_time = time.time()
    print(f"マルチスレッドを使用する場合 Time taken: {end_time - start_time} seconds")

    # マルチスレッドを使用しない場合
    start_time = time.time()
    simple_calculation_common(100000000)
    end_time = time.time()
    print(f"マルチスレッドを使用しない場合 Time taken: {end_time - start_time} seconds")

結果は以下の通りです。

マルチスレッドを使用する場合 Time taken: 1.9043524265289307 seconds
マルチスレッドを使用しない場合 Time taken: 1.866098403930664 seconds

マルチスレッドを使用しない場合の時間は短いです。

パフォーマンスが改善されない理由説明

PythonのGlobal Interpreter Lock(GIL)は、CPythonインタプリタ(Pythonの標準実装)において、同時に複数のスレッドがPythonバイトコードを実行することを制限する仕組みです。GILは、Pythonインタプリタがスレッドセーフであることを確保するものですが、同時に複数のCPUコアを効果的に活用することが難しくなります。このため、CPUバウンドな処理(主に計算処理)を行う場合には、マルチスレッドを利用してもパフォーマンスが改善されないことがあります。

代替手段

multiprocessingを利用することをお勧めします

import multiprocessing

def simple_calculation(_):  # 引数を受け取るように変更
    result = 0
    for _ in range(int(100000000 / 5)):
        result += 1
    return result

def perform_calculations(processes):
    with multiprocessing.Pool(processes=processes) as pool:
        results = pool.map(simple_calculation, range(processes))

if __name__ == "__main__":
    import time

    processes = 5  # 5つのプロセスを使用
    start_time = time.time()
    perform_calculations(processes)
    end_time = time.time()

    print(f"マルチプロセスを使用する場合 Time taken: {end_time - start_time} seconds")

実行結果は以下の通りです。

マルチプロセスを使用する場合 Time taken: 0.555854082107544 seconds

multiprocessing モジュールを使用する場合、プロセスは独立して動作し、GILの制約を受けずに複数のCPUコアを活用できます。CPUバウンドな処理においては、通常、プロセスベースの並列処理が効果的です。

このため、特に計算密度が高くCPUを多く必要とするタスクにおいては、GILの影響を避けるためにマルチプロセスを検討することが一般的です。

bisect

bisect モジュールは、ソートされたシーケンス内で要素を挿入したり、要素がどこに挿入されるべきかを見つけるためのモジュールです。このモジュールは効率的に挿入ポイントを見つけるために二分探索アルゴリズムを使用します。

import bisect

# ソートされたリストを作成
sorted_list = [1, 3, 3, 5, 7, 9, 11]

# 挿入ポイントを見つける
insert_point_left = bisect.bisect_left(sorted_list, 3)
insert_point_right = bisect.bisect_right(sorted_list, 3)

print("Insert Point (Left):", insert_point_left)
print("Insert Point (Right):", insert_point_right)

関数は第一級オブジェクトとして扱われる

Pythonにおいて関数は第一級オブジェクトとして扱われるため、関数を変数に代入し、引数や戻り値として使用することができます。以下にいくつかの例を挙げてみましょう。

1.関数を変数に代入

def greet(name):
    return f"Hello, {name}!"

# 関数を変数に代入
greeting_function = greet

# 変数経由で関数を呼び出し
result = greeting_function("John")
print(result)  # Output: Hello, John!

2.関数を他の関数の引数として使用

def square(x):
    return x ** 2

def operate_on_numbers(func, num):
    return func(num)

# 関数を引数として渡す
result = operate_on_numbers(square, 5)
print(result)  # Output: 25

3.関数を他の関数の戻り値として使用

def create_multiplier(factor):
    def multiplier(x):
        return x * factor
    return multiplier

# 関数を戻り値として取得
double = create_multiplier(2)
triple = create_multiplier(3)

# 戻り値として取得した関数の使用
print(double(4))  # Output: 8
print(triple(4))  # Output: 12

4.リスト内包表記で関数を適用

def square(x):
    return x ** 2

numbers = [1, 2, 3, 4, 5]

# リスト内包表記で関数を適用
squared_numbers = [square(num) for num in numbers]
print(squared_numbers)  # Output: [1, 4, 9, 16, 25]

匿名関数

匿名関数は簡単でありながらも、コードを簡潔に保ちつつ、一時的な処理や簡単な計算を行うのに便利です。

numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)  # Output: [2, 4]

Pythonの特殊メソッド

Pythonの特殊メソッド(ダンダーメソッド)は、ダブルアンダースコア(__)で始まり、終わる名前を持つメソッドです。これらのメソッドは通常、クラスの振る舞いを制御するために使用されます。以下は、いくつかの主要な特殊メソッドの例とその機能の説明です:

init(self, ...)

インスタンスが生成される際に呼び出される初期化メソッド。オブジェクトの初期化を行います。

class MyClass:
  def __init__(self, value):
      self.value = value

obj = MyClass(42)

2.str(self)

str() 関数や print() 関数が呼び出された際に、オブジェクトの文字列表現を返します。

class MyClass:
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return f"MyClass with value: {self.value}"

obj = MyClass(42)
print(obj)  # Output: MyClass with value: 42

3.repr(self)

repr() 関数が呼び出された際に、オブジェクトの標準的な文字列表現を返します。

class MyClass:
    def __init__(self, value):
        self.value = value

    def __repr__(self):
        return f"MyClass({self.value})"

obj = MyClass(42)
print(repr(obj))  # Output: MyClass(42)

4.len(self)

len() 関数が呼び出された際に、オブジェクトの長さを返します。

class MyList:
    def __init__(self, items):
        self.items = items

    def __len__(self):
        return len(self.items)

my_list = MyList([1, 2, 3, 4])
print(len(my_list))  # Output: 4

5.getitem(self, key)

インデックスやキーでオブジェクトにアクセスする際に呼び出されます。

class MyList:
    def __init__(self, items):
        self.items = items

    def __getitem__(self, index):
        return self.items[index]

my_list = MyList([1, 2, 3, 4])
print(my_list[2])  # Output: 3

6.setitem(self, key, value):

インデックスやキーに値を代入する際に呼び出されます。

class MyList:
    def __init__(self, items):
        self.items = items

    def __setitem__(self, index, value):
        self.items[index] = value

my_list = MyList([1, 2, 3, 4])
my_list[2] = 99
print(my_list.items)  # Output: [1, 2, 99, 4]

7.delitem(self, key):

インデックスやキーでオブジェクトの要素を削除する際に呼び出されます。

class MyList:
    def __init__(self, items):
        self.items = items

    def __delitem__(self, index):
        del self.items[index]

my_list = MyList([1, 2, 3, 4])
del my_list[1]
print(my_list.items)  # Output: [1, 3, 4]

8.call(self, ...):

インスタンスが関数のように呼び出された際に呼び出されるメソッド。

class MultiplyBy:
    def __init__(self, factor):
        self.factor = factor

    def __call__(self, x):
        return x * self.factor

multiply_by_5 = MultiplyBy(5)
result = multiply_by_5(7)
print(result)  # Output: 35

9.iter(self):

イテレータとしてオブジェクトを使えるようにします。

class CountDown:
    def __init__(self, start):
        self.current = start

    def __iter__(self):
        return self

    def __next__(self):
        if self.current <= 0:
            raise StopIteration
        else:
            self.current -= 1
            return self.current + 1

countdown = CountDown(5)
for number in countdown:
    print(number)
# Output:
# 5
# 4
# 3
# 2
# 1

デコレータ

デコレータとは、すでにある関数に処理の追加や変更を行う為の機能です。
デコレータを使うことによって、既存の関数の中身を直接変更することなく、それらに機能を追加したり変更したりすることが出来るようになります。

import time

def timing_decorator(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(f"Function {func.__name__} took {end_time - start_time:.2f} seconds to run.")
        return result
    return wrapper

@timing_decorator
def slow_function():
    time.sleep(2)
    print("Function executed.")

slow_function()
# 実行結果: Function slow_function took 2.00 seconds to run.

デコレータのメリット

1.再利用性と拡張性: 装飾器は再利用可能であり、同じ機能を複数の関数やメソッドに適用できます。これにより、コードの再利用が促進され、同じパターンを複数の場所で拡張することが容易になります。
2.分離されたコード: 装飾器は関数やメソッドの本体から装飾機能を分離します。これにより、コードが単純で明確に保たれ、関心ごとの分離が実現されます。例えば、ログ出力、認証、キャッシュなどの機能が関数のコードから分離され、その関数がより純粋で保守しやすくなります。
3.効率向上: 装飾器はコードの一部分に機能を追加するため、重複コードを減少させ、コードの効率を向上させることができます。例えば、パフォーマンス計測や結果のキャッシュ化などがこれに該当します。
4.動的な変更: 装飾器は関数やメソッドを動的に変更することができます。これにより、実行時に機能を有効または無効にすることが可能で、柔軟性を高めます。
5.可読性向上: 装飾器はコードに追加的な機能を宣言的に適用するため、関数の本体がその目的に集中できます。これにより、コードの可読性が向上します。

9
10
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
10