repl.it
@randompast/

SophisticatedIdealStartup

Python

No description

fork
loading
Files
  • main.py
main.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import numpy as np
import time

# Timing
def with_timer(f):
    def wrapper(*args, **kwargs):
        start = time.time()
        for i in range(100):
            result = f(*args, **kwargs)
        total_time = time.time() - start
        print(f.__name__ + " took " + str(total_time*1000) + "ms")
        return result
    return wrapper

# Original correct function
def f0(A, x, tl):
    b = min(tl, A.shape[0])
    return A[0:b].dot(x[tl:tl - b:-1])

@with_timer
def f1(A, x, tl):
    b = min(tl, A.shape[1])
    return A[:,0:b].dot(x[tl:tl - b:-1])

@with_timer
def f2(A, x, tl):
    b = min(tl, A.shape[1])
    return np.einsum("ij,j->i", A[:, 0:b], x[tl:tl - b:-1] )

@with_timer
def f3(A, x, tl):
    b = min(tl, A.shape[1])
    return np.tensordot(A[:,0:b], x[tl:tl - b:-1], axes=([1],[0]))

@with_timer
def f4(A, x, tl):
    b = min(tl, A.shape[1])
    return A[:, 0:b].dot(x[tl:tl - b:-1])

def f5(A, x, tl):
    b = min(tl, A.shape[1])
    return A[:, 0:b].dot(x)

@with_timer
def f6(A, x, t):
    return np.fromiter( ( f0(A[i], x, t) for i in range(A.shape[0])), float )

@with_timer
def f7(A, x, tl):
    b = min(tl, A.shape[1])
    xclipped = x[tl:tl-b:-1]
    return (A[:, 0:b]*xclipped).sum(axis=1)

@with_timer
def f8(A, x, t):
    b = min(t, A.shape[1])
    x_new = x[t:t - b:-1]
    return f5(A, x_new, t)

np.random.seed(0);
A = np.random.random((1000, 1000))
x = np.random.random(10000)
t = 500

f1(A, x, t)
f2(A, x, t)
f3(A, x, t)
f4(A, x, t)
f6(A, x, t)
f7(A, x, t)
f8(A, x, t)
Fetching token
?