Moritz
Moritz

Reputation: 5408

Writing fast python code within a class; overhead throug self argument

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

Answers (1)

Raito
Raito

Reputation: 1573

If you're concerned about performance, you should :

  • Profile your code. See which functions are taking the most of time and try to optimize them (or apply some optimizations tricks, look at the third bullet).
  • In your case, you're concerned particulary about the overhead of the self parameter, if you want you can specify @classmethod decorator to remove the need of self. You can call now your functions : MyClass.make_qs(*args), ...
  • Because you have computations results, I think you should see about memoization. Maybe you are re-computing things that you've already computed. (Which is expensive with more than 50k of functions call imo).

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

Related Questions