Note that in some cases, dynamic imports are done to avoid loading a Django subsystem, large external library, or optional dependency until it is strictly necessary.
There are certainly still examples where none of those good reasons apply. On Sep 24, 2008, at 2:37 AM, 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/auth/models.py > > 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 -~----------~----~----~----~------~----~------~--~---