On Monday, September 18, 2017 at 3:08:11 AM UTC-7, Jeroen Demeyer wrote:
>
> On 2017-09-16 03:31, Nils Bruin wrote: 
> > So if you're finding you can't put a "from A import a" at the top-level 
> > and using it locally has noticeable cost (which can easily happen: 
> > imports have a significant cost, even for modules that are already 
> > present), then you could try to do a straight import and access the 
> > appropriate namespace. 
>
> But accessing the namespace is also slower than doing the "from ... 
> import ..." import: 
>
> sage: x = 42 
> sage: timeit("sage.structure.element.parent(x)", number=20, repeat=10^6) 
> 20 loops, best of 1000000: 143 ns per loop 
> sage: timeit("parent(x)", number=20, repeat=10^6) 
> 20 loops, best of 1000000: 47.7 ns per loop 
>
> Yes, that is where the name-space-tree-flattening tool

sage: import sage.structure.element as elm

comes in handy. With that I get:

sage: timeit("sage.structure.element.parent(x)", number=20, repeat=10^6) 
20 loops, best of 1000000: 143 ns per loop
sage: timeit("elm.parent(x)", number=20, repeat=10^6) 
20 loops, best of 1000000: 47.7 ns per loop
sage: timeit("parent(x)", number=20, repeat=10^6) 
20 loops, best of 1000000: 47.7 ns per loop

I'm very surprised we're getting exactly the same timings. I'm also a 
little suspicious there's no difference measured between the 2nd and 3rd 
test. I think it indicates it helps quite a bit.

Of course, if you *really* care about this overhead, you should make sure 
that a significant part of the loop lies in one function and that the 
function lookup is from a local variable (not a global). I'm hopeful that 
the above indicates that in most cases we can get acceptable performance 
with a renamed global import of the module, without having to rebind 
particular objects from it to global/local variables in a different module 
via a "from ... import ..." construction, and that in many cases this is 
enough to avoid the circular import problems we encounter otherwise.

 

-- 
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