top | item 22042482

(no title)

maksimum | 6 years ago

Compared to __slots__ (also Python 3.7.4)

Using your definition of class X

  %timeit x.w()
  313 ns ± 18.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Add __slots__

  class X:
    __slots__ = ('a')
    def w(z):
      a = z.a
      return a+a+a+a+a

  %timeit x.w()
  271 ns ± 7.13 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
About 14% less time.

discuss

order

kragen|6 years ago

Your computer is evidently faster than my phone; how fast was y() for you?

Also you are missing a comma in your would-be tuple.

Dylan16807|6 years ago

For what it's worth I got about 196 and 131 for the original y and w, and after adding __slots__ (with comma) I got 186 and 123.