Hi!

On 2017-09-16, Nils Bruin <nbr...@sfu.ca> wrote:
> On Friday, September 15, 2017 at 2:28:06 PM UTC-7, Travis Scrimshaw wrote:
>>> Why are you proposing local imports?  If python puts a performance penalty 
>>> on them (in fact a significant performance penalty), they should be 
>>> avoided, no?
>>>
>> It is a relatively small penalty, so when it is not in a tight loop, it 
>> is useful to break out of those cyclic imports (and has better locality of 
>> reference).

Here are some timings:

sage: def test_local():
....:     import gc
....:     return gc.isenabled()
....: 
sage: def test_conditional():
....:     global gc
....:     try:
....:         return gc.isenabled()
....:     except NameError:
....:         import gc
....:         return gc.isenabled()
....:         
sage: %timeit test_conditional()
The slowest run took 17.65 times longer than the fastest. This could mean that 
an intermediate result is being cached.
10000000 loops, best of 3: 122 ns per loop
sage: %timeit test_local()
The slowest run took 19.69 times longer than the fastest. This could mean that 
an intermediate result is being cached.
1000000 loops, best of 3: 555 ns per loop

Thus, if an import on module level were circular, one can
break the circle by making a *conditional* *global* import
*locally*. And it is substantially faster than an unconditional
local import.

The following variation of the conditional import is taken from
  https://wiki.python.org/moin/PythonSpeed/PerformanceTips
and is slightly easier to read, but also slightly slower than
test_conditional above:

sage: gc = None
sage: def test_conditional2():
....:     global gc
....:     if gc is None:
....:         import gc
....:     return gc.isenabled()
....: 
sage: %timeit test_conditional2()
The slowest run took 60.73 times longer than the fastest. This could mean that 
an intermediate result is being cached.
10000000 loops, best of 3: 133 ns per loop

Best regards,
Simon

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to