← Back to team overview

banking-addons-team team mailing list archive

Re: lp:~therp-nl/banking-addons/ba70-lp1231174-default_method_should_not_raise_at_module_installation_time into lp:banking-addons

 

Amazingly the one line check for context is faster then the twoline check, even though recreating the empty dictionary. The differences are however very small. It also does not make a big difference wether a context is passed or not.

See the little script below. I have no clue about why this is.

For the functional reasons outlined by Stephan, the twoline check might still be preferred.

=======================
script time_context.py:
=======================

#!/usr/bin/env python
def one_line_context(context=None):
    context = context or {}

def two_line_context(context=None):
    if context == None:
        context = {}

if __name__ == '__main__':
    import timeit
    # Test calling context method without context
    tics = timeit.timeit(
        'one_line_context()',
        setup='from __main__ import one_line_context',
        number=10000) 
    print 'Oneline withouth context takes %f tics' % (tics,)
    tics = timeit.timeit(
        'two_line_context()',
        setup='from __main__ import two_line_context',
        number=10000) 
    print 'Twoline withouth context takes %f tics' % (tics,)

    # Test calling context method with context
    context = {'testing': True}
    tics = timeit.timeit(
        'one_line_context(context=context)',
        setup='from __main__ import one_line_context, context',
        number=10000) 
    print '\nOneline with context takes %f tics' % (tics,)
    tics = timeit.timeit(
        'two_line_context(context=context)',
        setup='from __main__ import two_line_context, context',
        number=10000) 
    print 'Twoline with context takes %f tics' % (tics,)

    # Test calling context method with empty context
    empty_context = {}
    tics = timeit.timeit(
        'one_line_context(context=empty_context)',
        setup='from __main__ import one_line_context, empty_context',
        number=10000) 
    print '\nOneline with empty context takes %f tics' % (tics,)
    tics = timeit.timeit(
        'two_line_context(context=empty_context)',
        setup='from __main__ import two_line_context, empty_context',
        number=10000) 
    print 'Twoline with empty context takes %f tics' % (tics,)

    print '\nNow decide on performance results'


======================
Results of running it:

Oneline withouth context takes 0.002138 tics
Twoline withouth context takes 0.007042 tics

Oneline with context takes 0.002146 tics
Twoline with context takes 0.002944 tics

Oneline with empty context takes 0.003464 tics
Twoline with empty context takes 0.004271 tics

Now decide on performance results

-- 
https://code.launchpad.net/~therp-nl/banking-addons/ba70-lp1231174-default_method_should_not_raise_at_module_installation_time/+merge/187677
Your team Banking Addons Core Editors is subscribed to branch lp:banking-addons.


References