I am having some performance problems that have left me baffled. I am using Django with MySQL. I do the following:
1 . Send data from a webpage using Ajax.
2 . Receive the post data in Views.py and process it via many function calls, inserting data into MySQL. The very first thing that I do is set a Python variable timeStart. The very last thing that I do, before returning a response, is calculate the time to process the data:
processTime = time.clock() - timeStart
I send a response using render-to-response where the response is processTime.
3 . processTime is displayed on the webpage.
processTime comes to 1.5 seconds, but the total time from sending the data via Ajax to receiving a response is 13 seconds. 11.5 seconds to render a simple page and to send and receive data – that’s a long time. But then it gets weirder. I cut short the views.py function, almost immediately sending back a response. That cuts the processing time down from 1.5 seconds to almost 0. But it also cuts the total time down from 13 seconds to about 2 seconds. That would seem to indicate that it is, in fact, the data processing that is slow. However that does not explain that the calculated processTime only came to 1.5 seconds. I really do set startTime in the first line and calculate processTime in the last line. StartTime is not overwritten.
It’s almost as though there is some kind of clean up process or something associated with finishing up and sending a response, something that is dependent on the function calls. But what? Or I am doing something stupid with my time calculation? I have set debug = False.
EDIT – extra info:
The template only contains the word “hello” and {{result}}. I’m not returning any queryset, just processTime: {‘result’:processTime}
The time difference seems to perhaps come when I am using MySQL stored functions. Like it’s returning the result, but then still doing something????
The problem was in the way that I measured processTime. I needed to be using time.time(), not time.clock(). time.clock() told me that I had only spent 1.5 seconds, but that’s apparently just the CPU time, not the actual time. The actual time spent in view.py was more like 13 seconds, which corresponds to what I had measured for the complete transaction.