-
Notifications
You must be signed in to change notification settings - Fork 0
/
caching.py
42 lines (38 loc) · 1004 Bytes
/
caching.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
import numpy as np
import timeit
import matplotlib.pyplot as plt
import subprocess
def prob1():
n = 18
times = []
for i in xrange(n):
setup = """
import numpy as np
a=np.zeros(2**18)
"""
code = """
a[::2**"""+str(i)+"""] = 1
"""
times.append(sum(timeit.repeat(code, setup=setup, number = 10, repeat = 10))/10.)
plt.plot(np.arange(n/2),times[:n/2],marker = 'o')
plt.grid()
plt.xlabel("Step Size (powers of 2)")
plt.xticks(np.arange(n/2),["2^"+str(i) for i in xrange(n/2)])
plt.ylabel("Time")
plt.show()
def prob2():
"""
determines the L1, and L2 cache sizes
"""
times = []
subprocess.call(["gcc","-O3","-mtune=native","-o","cache_size","cache_size.c"])
for i in xrange(10, 20):
res = subprocess.check_output(["./cache_size","16" ,str(2**(i-10)),"10"])
times.append(float(res.split(" ")[0][:-1]))
plt.plot(times,marker='o')
plt.grid()
plt.xlabel("Array Size (bytes)")
plt.xticks(np.arange(10),["2^"+str(i+10) for i in xrange(10)])
plt.ylabel("Time")
plt.show()
prob2()