Hi, do you remember this one?
I've just made comparison of this code to Java, and Cython does, imo, well. The same edit distance loop is only, on average 25 percent faster on JVM.
Thanks.
Hi, do you remember this one?
I've just made comparison of this code to Java, and Cython does, imo, well. The same edit distance loop is only, on average 25 percent faster on JVM.
Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| def edit_dist_dp(str1, str2, m, n):
store = [[0 for x in range(n + 1)] for x in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
if i == 0:
store[i][j] = j
elif j == 0:
store[i][j] = i
elif str1[i - 1] == str2[j - 1]:
store[i][j] = store[i - 1][j - 1]
else:
store[i][j] = 1 + min(store[i][j - 1], store[i - 1][j],
store[i - 1][j - 1])
return store[m][n]
|
1
2
3
4
5
6
7
8
9
10
| import timeit
py = timeit.timeit('edit_distance.edit_dist_dp("asdsdhter", "dsfladrte", 9, 9)',
setup='import edit_distance', number=10000)
cy = timeit.timeit('edit_distance_cy.edit_dist_dp("asdsdhter", "dsfladrte", 9, 9)',
setup='import edit_distance_cy', number=10000)
print(f"Python and Cython times: {py}, Cython: {cy}")
print(f"Cython is {py/cy} x faster than Python")
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| def edit_dist_dp(str1, str2, m, n):
store = [[0 for x in range(n + 1)] for x in range(m + 1)]
cdef int i, j
for i in range(m + 1):
for j in range(n + 1):
if i == 0:
store[i][j] = j
elif j == 0:
store[i][j] = i
elif str1[i - 1] == str2[j - 1]:
store[i][j] = store[i - 1][j - 1]
else:
store[i][j] = 1 + min(store[i][j - 1], store[i - 1][j],
store[i - 1][j - 1])
return store[m][n]
|
1
2
| Python: 0.7150410660033231, Cython: 0.1197679779943428
Cython is 5.970219068381515 x faster than Python
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| cpdef int edit_dist_dp(str str1, str str2, int m, int n):
store = [[0 for x in range(n + 1)] for x in range(m + 1)]
cdef int i, j
for i in range(m + 1):
for j in range(n + 1):
if i == 0:
store[i][j] = j
elif j == 0:
store[i][j] = i
elif str1[i - 1] == str2[j - 1]:
store[i][j] = store[i - 1][j - 1]
else:
store[i][j] = 1 + min(store[i][j - 1], store[i - 1][j],
store[i - 1][j - 1])
return store[m][n]
|
1
2
3
| (project_venv) $ python tests.py
Python: 0.7169225399993593, Cython: 0.07916731700242963
Cython is 9.055789271946113 x faster than Python
|
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 | cpdef int edit_dist_dp(str str1, str str2, int m, int n): cdef int i, j, x, y cdef int store[50][50] for x in range(n + 1): for y in range(m + 1): store[x][y] = 0 for i in range(m + 1): for j in range(n + 1): if i == 0: store[i][j] = j elif j == 0: store[i][j] = i elif str1[i - 1] == str2[j - 1]: store[i][j] = store[i - 1][j - 1] else: store[i][j] = 1 + min(store[i][j - 1], store[i - 1][j], store[i - 1][j - 1]) return store[m][n] |
1 2 3 | (project_venv) $ python tests.py
Python: 0.7155497479980113, Cython: 0.027351742995961104
Cython is 26.16102922960606 x faster than Python
|