Skip to content

Instantly share code, notes, and snippets.

@vmoens
Created February 28, 2025 15:34
Show Gist options
  • Select an option

  • Save vmoens/5c9fb9d1572ddb10b15d762b1dea3e49 to your computer and use it in GitHub Desktop.

Select an option

Save vmoens/5c9fb9d1572ddb10b15d762b1dea3e49 to your computer and use it in GitHub Desktop.
import_speed
import timeit
# Create a simple module structure for demonstration purposes
class Module:
class Package:
class Subpack:
def func(self):
pass
module = Module()
def test_long_chain():
module.Package.Subpack().func()
from_module = module.Package.Subpack()
def test_short_chain():
from_module.func()
from_module_imp = module.Package.Subpack().func
def test_imported_func():
from_module_imp()
long_chain_time = timeit.timeit(test_long_chain, number=1000000)
short_chain_time = timeit.timeit(test_short_chain, number=1000000)
imported_func_time = timeit.timeit(test_imported_func, number=1000000)
print(f"Long chain: {long_chain_time:.6f} seconds")
print(f"Short chain: {short_chain_time:.6f} seconds")
print(f"Imported func: {imported_func_time:.6f} seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment