Skip to Content
『Learning Python 第6版』
book

『Learning Python 第6版』

by Mark Lutz
March 2025
Intermediate to advanced
1172 pages
24h 57m
Japanese
O'Reilly Media, Inc.
Content preview from 『Learning Python 第6版』

第21章. ベンチマーキングの間奏曲

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

さて、関数コーディングと反復ツールについて十分に検討したので、この2つを実際に使ってみるために少し寄り道をしよう。この章では、標準的なPythonとその代替ツールの両方において、これまでに出会った反復ツールの相対的な性能を測定する大規模なケーススタディを行い、本書の関数パートを締めくくる。

このケーススタディでは、Pythonのコードタイミングツールを調査し、一般化ベンチマークテクニックについて議論し、ここまでに見てきたものよりも現実的で有用なコードを開発する。また、使用したコードの速度も測定する。このデータ点は、あなたのプログラミングの目的によって重要であったりそうでなかったりする。

最後に、この章は本書の最終章なので、これまで読んできたアイデアのコーディングを始めるのに役立つ、いつもの「ゴッチャ」と練習問題のセットで締めくくる。まずは、具体的なPythonコードで楽しんでみよう。

国産ツールによるベンチマーキング

本書では、イテレーションの代替案()をいくつか紹介してきた。プログラミングの多くと同様、これらはトレードオフを表している。表現力のような主観的なオブジェクトと、パフォーマンスのような客観的な基準の両方だ。プログラマやエンジニアとしてのあなたの仕事の一部は、このような要素に基づいてツールを選択することである。

性能に関しては、リスト内包表記やmap 呼び出しが、for ループ文よりも速度面で有利な場合があることを、本書では何度か述べてきた。また、ソートの速度は順序によって変化すること、前章のジェネレータ関数と式は、他のすべての関数よりも遅い傾向があることを述べてきた。しかし、これらの関数は必要なメモリ空間を最小限に抑え、生成する結果が多い場合に呼び出し元を結果生成のために遅延させることはない。

しかし、ベンチマークにはいくつかの大きな注意点がある。Pythonの内部は常に変更され、最適化されているため、Pythonの性能は時間とともに変化する可能性がある。第2章で述べたように、例えば、標準的なCPythonは将来標準的なJITを採用する可能性があり、それがコンテキストによっては速度を変える可能性がある。

要するに、自分自身でスピードを確かめたいのであれば、自分のデバイスで、使う予定のPythonを使って、今ここで、コードのタイムを計る必要がある。修飾語が多いが、ベンチマークは経験的な作業だ。

タイマーモジュール:テイク1

幸運なことに、Pythonはカスタムツールで。例えば、任意の位置の引数を持つ関数の複数呼び出しにかかる合計時間を求めるには、例21-1のモジュールファイルでコード化された関数で十分だろう。

例21-1. timer0.py
"Simplistic timing function"

import time
def timer(func, *args):                    # Any positional arguments (only)
    start = time.perf_counter()
    for i in range(100_000):               # Hardcoded reps, range() timed
        func(*args)
    return time.perf_counter() - start     # Total elapsed time in seconds
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Python Polars ―高速なデータ処理を実現する次世代ライブラリ

Python Polars ―高速なデータ処理を実現する次世代ライブラリ

Jeroen Janssens, Thijs Nieuwdorp, 中田 秀基
入門 Python 3 第2版

入門 Python 3 第2版

Bill Lubanovic, 鈴木 駿, 長尾 高弘

Publisher Resources

ISBN: 9798341634367