I hav used the following code to display a result and time elapsed to find out the result in mod_wsgi.But watever i try i always get the elapsed time as 0 even though i rounded the time to nanoseconds. What do i do now to calculate the difference?
#!D:/Python27/python
from wsgiref.simple_server import make_server
from cgi import parse_qs, escape
import time
import datetime
html = """
<html>
<body>
<h2>Sum of first n factorials</h2>
<form method="get" action="Sec1q10.py">
Number :<input type="text" name="t1">
<br> <input type="submit" value="Submit">
</form>
<br>
Result:%s<br>
Now:%s<br>
End:%s<br>
Time:%s
</body>
</html>"""
def application(environ, start_response):
d=parse_qs(environ['QUERY_STRING'])
now = int(round(time.time()*1000000))
n=d.get('t1',[''])[0]
i=1
p=1
s=0
while (i<=int(n)):
p = p*i
s =s+p
i = i+1
end= int(round(time.time()*1000000))
diff=end-now
status = '200 OK'
output = html % (s or 'Empty',now or 'Empty',end or 'Empty',diff or 'Empty')
response_headers = [('Content-type', 'text/html'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
and the output it shows is if i entered a number 4:
Result:33
Now:1346520500489000
End:1346520500489000
Time:Empty
wat else can i use to find out the time elapsed?
time.time()does not have infinite resolution — the operation that you’re testing takes next to no time at all. Try using a much largern, or repeating the loop a large fixed number of times (e.g, a thousand) and dividing the result by the number of times you repeated it.