-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtiming.py
More file actions
30 lines (25 loc) · 734 Bytes
/
timing.py
File metadata and controls
30 lines (25 loc) · 734 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python
"""
timing example
"""
def primes_stupid(N):
"""
a really simple way to compute the first N prime numbers
"""
primes = [2]
i = 3
while len(primes) < N:
for j in range(2, i/2): # the "/2" is an optimization -- no point in checking even numbers
if not i % j: # it's not prime
break
else:
primes.append(i)
i += 1
return primes
if __name__ == "__main__":
import timeit
print "running the timer:"
run_time = timeit.timeit("primes_stupid(5)",
setup="from __main__ import primes_stupid",
number=100000) # default: 1000000
print "it took:", run_time