15.12.17 18:36, Antoine Pitrou пише:
Do you have any general idea how to speed up class creation?

Some work was done in [https://bugs.python.org/issue31336]. Currently I have no ideas.

Creating a class is 1-2 orders slower than creating a function. And adding parent classes significantly slows down it.

$ ./python -m perf timeit --duplicate=100 'def f(s): pass'
.....................
Mean +- std dev: 50.4 ns +- 0.8 ns
$ ./python -m perf timeit --duplicate=100 'class C: pass'
.....................
Mean +- std dev: 6.80 us +- 0.14 us
$ ./python -m perf timeit --duplicate=100 'class C:' '  def m(s): pass'
.....................
Mean +- std dev: 7.11 us +- 0.11 us
$ ./python -m perf timeit --duplicate=100 'class C(str): pass'
.....................
Mean +- std dev: 8.47 us +- 0.34 us

I'm surprised that that creating a method is much slower (6 times!) than creating a function. Maybe due to __set_name__ or other magic.

It isn't surprised that creating an enum or namedtuple class is much slower than creating a regular class. The latter was much worse before 3.7.

$ ./python -m perf timeit -s 'from enum import Enum' --duplicate=100 'class E(Enum): A = 1'
.....................
Mean +- std dev: 45.9 us +- 0.8 us
$ ./python -m perf timeit -s 'from collections import namedtuple' --duplicate=100 'P = namedtuple("P", ("x",))'
.....................
Mean +- std dev: 44.7 us +- 0.6 us

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to