Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimization Techniques in Python

Tags:

python

Recently i have developed a billing application for my company with Python/Django. For few months everything was fine but now i am observing that the performance is dropping because of more and more users using that applications. Now the problem is that the application is now very critical for the finance team. Now the finance team are after my life for sorting out the performance issue. I have no other option but to find a way to increase the performance of the billing application.

So do you guys know any performance optimization techniques in python that will really help me with the scalability issue

Guys we are using mysql database and its hosted on apache web server on Linux box. Secondly what i have noticed more is the over all application is slow and not the database transactional part. For example once the application is loaded then it works fine but if they navigate to other link on that application then it takes a whole lot of time.

And yes we are using HTML, CSS and Javascript

like image 793
fear_matrix Avatar asked Mar 30 '10 14:03

fear_matrix


People also ask

What is optimization technique?

Introduction: In optimization of a design, the design objective could be simply to minimize the cost of production or to maximize the efficiency of production. An optimization algorithm is a procedure which is executed iteratively by comparing various solutions till an optimum or a satisfactory solution is found.

What is code optimization and its techniques?

The code optimization in the synthesis phase is a program transformation technique, which tries to improve the intermediate code by making it consume fewer resources (i.e. CPU, Memory) so that faster-running machine code will result.


3 Answers

As I said in comment, you must start by finding what part of your code is slow.

Nobody can help you without this information.

You can profile your code with the Python profilers then go back to us with the result.

If it's a Web app, the first suspect is generally the database. If it's a calculus intensive GUI app, then look first at the calculations algo first.

But remember that perf issues car be highly unintuitive and therefor, an objective assessment is the only way to go.

like image 139
e-satis Avatar answered Nov 13 '22 14:11

e-satis


ok, not entirely to the point, but before you go and start fixing it, make sure everyone understands the situation. it seems to me that they're putting some pressure on you to fix the "problem".

well first of all, when you wrote the application, have they specified the performance requirements? did they tell you that they need operation X to take less than Y secs to complete? Did they specify how many concurrent users must be supported without penalty to the performance? If not, then tell them to back off and that it is iteration (phase, stage, whatever) one of the deployment, and the main goal was the functionality and testing. phase two is performance improvements. let them (with your help obviously) come up with some non functional requirements for the performance of your system.

by doing all this, a) you'll remove the pressure applied by the finance team (and i know they can be a real pain in the bum) b) both you and your clients will have a clear idea of what you mean by "performance" c) you'll have a base that you can measure your progress and most importantly d) you'll have some agreed time to implement/fix the performance issues.

PS. that aside, look at the indexing... :)

like image 42
rytis Avatar answered Nov 13 '22 12:11

rytis


A surprising feature of Python is that the pythonic code is quite efficient... So a few general hints:

  • Use built-ins and standard functions whenever possible, they're already quite well optimized.
  • Try to use lazy generators instead one-off temporary lists.
  • Use numpy for vector arithmetic.
  • Use psyco if running on x86 32bit.
  • Write performance critical loops in a lower level language (C, Pyrex, Cython, etc.).
  • When calling the same method of a collection of objects, get a reference to the class function and use it, it will save lookups in the objects dictionaries (this one is a micro-optimization, not sure it's worth)

And of course, if scalability is what matters:

  • Use O(n) (or better) algorithms! Otherwise your system cannot be linearly scalable.
  • Write multiprocessor aware code. At some point you'll need to throw more computing power at it, and your software must be ready to use it!
like image 44
fortran Avatar answered Nov 13 '22 12:11

fortran