Reputation: 5408
Do I produce overhead by passing the self
argument within a class ?
I wrote a class for fitting my data. Inside the class are several helper functions and all together form the system of equations to fit. The convenient thing is the re-usability and I can do something like myclass.get_data()
and my_class.fit_data()
etc. However, I am concerned that the way I implemented my code may produce overhead (some of the functions are called about 50000 times). E.g.:
Following function calculates one variable in my system:
def get_qs(self,x,cp,cs):
qs = fsolve(self.make_qs,50.,args = (x,cp,cs))
return(qs)
It calls make_qs
:
def make_qs(self,qs,x,cp,cs):
q0 = x[3*self.n_comp]
# generates the implicit equation for qs (capacity of salt)
denom = self.get_denom(qs,x,cp,cs)
F = q0*cs/denom - qs
return(F)
Which itself calls get_denom
def get_denom(self,qs,x,cp,cs):
k = x[0:self.n_comp]
sigma = x[self.n_comp:2*self.n_comp]
z = x[2*self.n_comp:3*self.n_comp]
# calculates the denominator in Equ 14a - 14c (Brooks & Cramer 1992)
a = (sigma + z)*( k*(qs/cs)**(z-1) )*cp
denom = np.sum(a) + cs
return denom
I thought about writing the functions in Fortran and just pass the final result to the cost function within Python.
Upvotes: 1
Views: 244
Reputation: 1573
If you're concerned about performance, you should :
MyClass.make_qs(*args)
, ...I think that passing self produce a very very very very very very [...] low overhead even for more than 50k+ calls, because look at the Web app in Python, they use not one but a lot of classes (with "self overhead") and they can serve billions of requests very quickly.
Upvotes: 1