Being curious, had tried several benchmarks as well. Indeed, the function-level imports have some overhead. But the overhead becomes less and less important, as long as the function complexity increases. If a function does nothing except import (as in your case), the performance hit is several hundred percents. If a function does at least a crypto hash calculation, the performance loss is about 25% (~7.3 seconds versus 5.4 seconds for 1 million iterations). If a function does three different hash calculations, the performance loss is mere 6%: 26.5 seconds vs 24.8 (with the same conditions). So yes, the in-function imports have their cost. On the other hand, if different functions in a module import different other modules, there will be drawbacks (for moving the imports to the head of the module) too: with the function-level imports, we import only the modules we need, and only when we need them. With the module-level imports, we import everything (any module function ever needs) when calling any function from the module. Light startup/heavy function calls in the first case; heavy startup/light function calls in the second one. So, neither approach is a silver bullet. The consideration is needed for any module/function to decide what is better for them.
On 24 сент, 11:37, David Cramer <[EMAIL PROTECTED]> wrote: > I was digging through some code today, and I noticed imports are > happening within a lot of functions. It was my knowledge that it works > like so: > > import in a function is the same as ruby's load or php's include -- > its executed everytime the function is > > import in a module outside a function is like ruby's require or php's > require_once -- its loaded and cached in memory > > If this is the case, then this seems like an unneeded performance hits > in many situations. > > An example off hand, is the get_hexdigest() function of the auth > module:http://code.djangoproject.com/browser/django/trunk/django/contrib/aut... > > It waits to import the hashing methods, which if you're in a pooled > environment, it could save a lot by onlying importing it once, > correct? > > Here's my benchmark script: > > import timeit > def without_import(): > pass > def with_import(): > import random > > def main(): > print "Without an inner import:", > print timeit.Timer("module_loading.without_import()", "import > module_loading, random").timeit() > print "With an inner import:", > print timeit.Timer("module_loading.with_import()", "import > module_loading").timeit() > > if __name__ == '__main__': > main() > > And the result: > > Without an inner import: 1.28283500671 > With an inner import: 5.402146101 > > If everyone's in agreement, I'd like to approach correcting this in > Django --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~----------~----~----~----~------~----~------~--~---