6 / 6
Oct 2014

Can you help me.I am relatively new to programming.I have solved 6 problems in classical set out of which 4 have shown TLE.I need help to find the time of running of python code and also profiling it so that I can find the time of different portions of my code.I write my code in notepad++ and run it on windows powershell.Your inputs would be immensly helpful to me !!

  • created

    Jul '14
  • last reply

    Oct '14
  • 5

    replies

  • 589

    views

  • 4

    users

  • 3

    links

I'm a relative newbie to Python, so I am not an expert whatsoever. That being said, the following link had some great information:

stackoverflow.com/questions/5823 ... hon-script6

I think that will show you, in a general way, where your time, is being spent.

In addition, perhaps the timeit module may also be of assistance?

docs.python.org/3.0/library/timeit.html

It appears it is only meant for small snippets but maybe one who is more versed in Python than I can suggest if one can measure function execution times with it?

thanks all for your inputs.I read extensively but i still can not identify the problems.For example , I wrote the following code for problem FACTORIAL.Is there any alternate shorter way of input??

import math
num = []
append = num.append
n = int(raw_input()) #the number of test cases
for i in range(0, n):
x = int(raw_input())
append(x) #append all the inputs into list num[]

for j in range(0,n):
flag = 0
fact = math.factorial(num[j]) #finding the factorial
while (fact%10) ==0: #checking how many times the number is divisible by 10
flag = flag + 1
fact = fact/10
print flag.For example , I wrote the following code for problem FACTORIAL.

First: Use code-tags when posting code!
Without indentation your Python code is wrong.

Second: Even without identation the problem can easily be identified.
It's not a "Python is too slow"-problem, but a "algorithm is not well designed"-problem.
Just do some experimenting in the Python shell and calculate factorial of, say, 100, 1000, etc.
Then look at the problem constraints and after that think about a new approach ...

3 months later

This is a relatively simple way of doing this.

First of all, import time at the top of your code. Then, at the point that you want to start timing an operation, put the following:

start = time.clock()

At the end of what you think your problem section is, put this...

runtime = time.clock() - start
print runtime

This should tell you how long that section took to run.