Skip to content

Instantly share code, notes, and snippets.

@Infernio
Created August 23, 2020 17:24
Show Gist options
  • Save Infernio/f3a2cb592afe805c5ff691f79f8e8e5d to your computer and use it in GitHub Desktop.
Save Infernio/f3a2cb592afe805c5ff691f79f8e8e5d to your computer and use it in GitHub Desktop.
PS C:\Users\Infernio> py -2 -m timeit -s """
>> class Foo(object):
>> bar = 'test'
>>
>> o = Foo()
>> getter = o.__getattribute__
>> """ "getter('bar')"
10000000 loops, best of 3: 0.044 usec per loop
PS C:\Users\Infernio> py -2 -m timeit -s """
>> class Foo(object):
>> bar = 'test'
>>
>> o = Foo()
>> getter = o.__getattribute__
>> """ "getattr(o, 'bar')"
10000000 loops, best of 3: 0.0533 usec per loop
PS C:\Users\Infernio> py -2 -m timeit -s """
>> class Foo(object):
>> bar = 'test'
>>
>> o = Foo()
>> getter = o.__getattribute__
>> """ "o.__getattribute__('bar')"
10000000 loops, best of 3: 0.0741 usec per loop
PS C:\Users\Infernio> py -3 -m timeit -s """
>> class Foo(object):
>> bar = 'test'
>>
>> o = Foo()
>> getter = o.__getattribute__
>> """ "getter('bar')"
10000000 loops, best of 5: 37 nsec per loop
PS C:\Users\Infernio> py -3 -m timeit -s """
>> class Foo(object):
>> bar = 'test'
>>
>> o = Foo()
>> getter = o.__getattribute__
>> """ "getattr(o, 'bar')"
10000000 loops, best of 5: 31.6 nsec per loop
PS C:\Users\Infernio> py -3 -m timeit -s """
>> class Foo(object):
>> bar = 'test'
>>
>> o = Foo()
>> getter = o.__getattribute__
>> """ "o.__getattribute__('bar')"
5000000 loops, best of 5: 58.6 nsec per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment