Class instance error in Python 3 than Python 2

I accidentally noticed that a simple program that creates a class from a large data file works much faster in Python 2.7 compared to 3.5. I read here that using “infinite precision” integers is to blame for the slowdown in a simple enumeration, but even when I tried a simple test that instantiates this class, I found that Python 3 was significantly slower:

class Benchmark(object): def __init__(self): self.members = ['a', 'b', 'c', 'd'] def test(): test = Benchmark() if __name__ == '__main__': import timeit print(timeit.timeit("test()", setup="from __main__ import test")) 

I thought that maybe this was due to the size of each instance of the class, but the Python 3 instance was smaller than 2 (56 vs. 64).

 $python3 benchmarks.py 0.7017288669958361 $python benchmarks.py 0.508942842484 

I tried many variations of this topic, including with 3.4 on another machine, and still get the same results. Any ideas what is going on?

+5
source share
1 answer

You do not measure the time of creating an instance of a class, you measure the instance of a class, plus purpose, plus creating a list, ...

Here is the correct criterion:

 $ python -m timeit -s 'class C(object): pass' 'C()' 10000000 loops, best of 3: 0.0639 usec per loop $ python3 -m timeit -s 'class C(object): pass' 'C()' 10000000 loops, best of 3: 0.0622 usec per loop 

As you can see, Python 3 looks faster.

+3
source

Source: https://habr.com/ru/post/1235382/


All Articles