CrazyMonkeyFox
August 20th, 2009, 02:43 PM
I have been writing quite a few small python scripts lately, and I am interested in finding out the time it takes for these scripts to execute. I am aware of the time module, but unsure how to implement it, also whether its the most reliable or not.
I include a script that gets the sum of all primes below 2 million here. If I wanted to time it, how would I go about it?
#!/usr/bin/env python
def isprime(n):
for x in range(2, int(n**0.5)+1):
if n % x == 0:
return False
return True
sum = 0
for i in range (3, 2000000, 2):
if isprime(i) == True: sum += i
sum = sum + 2
print sumThanks in advance :)
I include a script that gets the sum of all primes below 2 million here. If I wanted to time it, how would I go about it?
#!/usr/bin/env python
def isprime(n):
for x in range(2, int(n**0.5)+1):
if n % x == 0:
return False
return True
sum = 0
for i in range (3, 2000000, 2):
if isprime(i) == True: sum += i
sum = sum + 2
print sumThanks in advance :)