is python exception slow?
I’m kind using python exceptions to structure programm follow in my web application, and I’m wondering how throwing exceptions will affect performance of my application. what is your thoughts?
which one of the following statements is less expensive in terms of memory and cpu?
try:
artist = Artist.objects.get(id=id)
except:
raise Http404
artist = Artist.objects.filter(id=id)
if not artist:
return HttpResponse('404')
Handling exceptions will be the least of your worries with regards to performance. I would suggest, however, that you use a shortcut provided by Django for you:
Which either assigns the object to
artistor returns a 404. It’s win-win!Also, you should check out the django-debug-toolbar, which provides rendering/cpu time, context switches, and all kinds of other helpful tidbits of data for developers and might be just the tool you need.