-
Notifications
You must be signed in to change notification settings - Fork 1
/
walk.py
50 lines (36 loc) · 1.39 KB
/
walk.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import math
import random
import requests
def use_memory(str_repeat=10000, str_count=1000):
hello_list = []
for _ in range(str_count // 2):
hello_list.append('Hello world! ' * str_repeat)
for _ in range(str_count // 2):
hello_list.append('I am profiling! ' * str_repeat)
def random_download():
random_word = ''.join(random.sample('abcdefghijklmnopqrstuvwxyz', k=10))
requests.get('http://www.example.com/' + random_word)
def primes_in_range(n):
"""Use Sieve of Eratosthenes find all primes in range 1..n"""
is_prime = [True] * n # is_prime[i] tells if i + 1 is a prime number
is_prime[0] = False
for prime_candidate in range(2, int(math.sqrt(n)) + 1):
if is_prime[prime_candidate - 1]:
# mark every multiple of prime_candidate as non-prime
for k in range(2, (n // prime_candidate) + 1):
non_prime = prime_candidate * k
assert non_prime <= n
is_prime[non_prime - 1] = False
primes = []
for i, i_is_prime in enumerate(is_prime):
if i_is_prime:
primes.append(i + 1)
return primes
def keep_python_busy(compute_repeat=6, io_repeat=3):
for _ in range(compute_repeat):
n = random.randint(1000000, 1200000)
primes_in_range(n)
for _ in range(io_repeat):
random_download()
if __name__ == '__main__':
keep_python_busy()