Do both of these programs consume the same memory and time?
def conv(txt):
a=int(txt) #Does the variable "a" consumes extra memory?
return a
print(conv("4"))
def conv(txt):
return int(txt)
print(conv("4"))
I am a beginner in python, so I couldn’t understand how the memory allocation works when implementing method on a piece of data in python.
Success is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
In the first program, a variable called “a” is created to store the integer conversion while in the second program, the integer conversion is directly returned without creating an extra variable. Creating the extra variable in the first program takes up a tiny bit more memory.
In terms of time, both programs are likely to be very similar in speed because the difference in how they handle the conversion is quite small. So, for practical purposes, you can consider them to be almost the same in terms of memory and time usage.
Bijoy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.