Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Performance of attribute lookup for type objects #92216

Closed
Labels
3.12only security fixesperformancePerformance or resource usagetype-bugAn unexpected behavior, bug, or error
@eendebakpt

Description

@eendebakpt

Bug report

The performance of attribute lookup for type objects is worse than for other objects. A benchmark

import pyperfrunner=pyperf.Runner()setup="""class Class:    def all(self):        passx=Class()"""runner.timeit('hasattr x.all', "hasattr(x, 'all')", setup=setup)runner.timeit('hasattr x.__array_ufunc__', "hasattr(x, '__array_ufunc__')", setup=setup)runner.timeit('hasattr Class.all', "hasattr(Class, 'all')", setup=setup)runner.timeit('hasattr Class.__array_ufunc__', "hasattr(Class, '__array_ufunc__')", setup=setup) # worse performance

Results:

hasattr x.all: Mean +- std dev: 68.1 ns +- 1.1 nshasattr x.__array_ufunc__: Mean +- std dev: 40.4 ns +- 0.3 nshasattr Class.all: Mean +- std dev: 38.1 ns +- 0.6 nshasattr Class.__array_ufunc__: Mean +- std dev: 255 ns +- 2 ns

The reason seems to be that thetype_getattro always executesPyErr_Format, wheras for the "normal" attribute lookup this is avoided (seehere andhere)

Notes:

Your environment

  • CPython versions tested on: Python 3.11.0a7+
  • Operating system and architecture: Linux Ubuntu

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixesperformancePerformance or resource usagetype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp