Skip to content

Instantly share code, notes, and snippets.

@susmithagudapati
Last active April 28, 2020 18:26
Show Gist options
  • Save susmithagudapati/a524a98896d56d089015e84534ca2ef9 to your computer and use it in GitHub Desktop.
Save susmithagudapati/a524a98896d56d089015e84534ca2ef9 to your computer and use it in GitHub Desktop.
# slower because it loads all products into Python memory to execute 'len()'
%timeit len(Product.objects.all())
3.47 ms ± 36 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
# faster because an SQL expression executes this
%timeit Product.objects.count()
267 µs ± 7.51 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment