"import" inside a function or method should be cached
Bug #155076 reported by
Carl Witty
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Cython |
Won't Fix
|
Wishlist
|
Unassigned |
Bug Description
Compared to normal Cython code, import is very slow. And in a normal program, at every callsite, import will return the same object each time it is called.
It would be nice if Cython's import would cache the return value and just use the same value on subsequent calls. (This does reduce Python compatibility in case somebody is playing games with __builtins_
To post a comment you must log in.
On my system, an import seems to take about 2 microseconds. This isn't much, but I've recently been working on optimizing one particular chunk of Cython code, managing to bring it from 1.2 milliseconds to 500 microseconds. On this scale, every microsecond counts.
Here are my benchmark results:
sage: %timeit test1()
10 loops, best of 3: 20.2 ms per loop
sage: %timeit test2()
10000000 loops, best of 3: 111 ns per loop
test1() does 10,000 imports; test2() probably does nothing (I haven't checked to see if the compiler optimized away my 10,000-count empty loop, but it probably did).
The benchmark is attached to this comment.