I am wondering would this make any real efficieny difference (ie computation time, memory etc..)
This is my model:
class FooUser(models.Model):
name = models.CharField(max_length=50)
sirname = models.CharField(max_length=50)
Assume I have 2 different approaches while saving a FooUser at a view:
First one, assigning retrieved values to a variable and pass it to the object after that.
#say I retrieve name and sirname from users cookie.(lets not care for the exceptions for now.
input_name =request.session['name']
input_sirname =request.session['sirname']
FooUser(name=input_name,sirname=input_sirname).save()
Second one, directly passing as parameter:
#say I retrieve name and sirname from users cookie.(lets not care for the exceptions for now.
FooUser(name=request.session['name'],sirname=request.session['sirname']).save()
I know this question can be a little stupid but for long inputs, passing these inputs to the object makes the code almost unreadable 🙂
This:
is not copying strings to variables. It’s only assigning pointers to string objects to names in local dictionary (input_name, input_sirname). For better explanation you can take a loot at this: http://effbot.org/zone/python-objects.htm.
Writing this, having those intermediate dictionary entries (input_name, input_sirname) has such low overhead in 99,999% of cases, that I bet you have some other bottlenecks in your program you should focus on.
And remember: premature optimization is the root of all evil 🙂