Skip to content

Instantly share code, notes, and snippets.

@susmithagudapati
Last active April 28, 2020 18:25
Show Gist options
  • Save susmithagudapati/365b14a1f9824eba7ee2f6ab15773f9e to your computer and use it in GitHub Desktop.
Save susmithagudapati/365b14a1f9824eba7ee2f6ab15773f9e to your computer and use it in GitHub Desktop.
# takes more time because it loads the whole model object into Python memory
%timeit upcs = [product.upc for product in Product.objects.all()]
3.91 ms ± 131 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
# faster than the above code because an SQL expression executes this
%timeit Product.objects.values_list('upc', flat=True)
32.3 µs ± 1.12 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment