12
12
the following:
13
13
14
14
* :func:`dot_product <td3a_cpp.tutorial.dot_cython.dot_product>`
15
- * :func:`dot_cython_array <td3a_cpp.tutorial.dot_cython.dot_cython_array>`
16
- * :func:`dot_cython_array_optim
17
- <td3a_cpp.tutorial.dot_cython.dot_cython_array_optim >`
18
- * :func:`dot_array <td3a_cpp.tutorial.dot_cython.dot_array >`
19
- * :func:`dot_array_16 <td3a_cpp.tutorial.dot_cython.dot_array_16 >`
20
- * :func:`dot_array_16_sse <td3a_cpp.tutorial.dot_cython.dot_array_16_sse >`
15
+ * :func:`ddot_cython_array <td3a_cpp.tutorial.dot_cython.dot_cython_array>`
16
+ * :func:`ddot_cython_array_optim
17
+ <td3a_cpp.tutorial.dot_cython.ddot_cython_array_optim >`
18
+ * :func:`ddot_array <td3a_cpp.tutorial.dot_cython.ddot_array >`
19
+ * :func:`ddot_array_16 <td3a_cpp.tutorial.dot_cython.ddot_array_16 >`
20
+ * :func:`ddot_array_16_sse <td3a_cpp.tutorial.dot_cython.ddot_array_16_sse >`
21
21
22
22
.. contents::
23
23
:local:
27
27
import matplotlib .pyplot as plt
28
28
from pandas import DataFrame , concat
29
29
from td3a_cpp .tutorial .dot_cython import (
30
- dot_product , dot_cython_array ,
31
- dot_cython_array_optim , dot_array ,
32
- dot_array_16 , dot_array_16_sse
30
+ dot_product , ddot_cython_array ,
31
+ ddot_cython_array_optim , ddot_array ,
32
+ ddot_array_16 , ddot_array_16_sse
33
+ )
34
+ from td3a_cpp .tutorial .dot_cython import (
35
+ sdot_cython_array ,
36
+ sdot_cython_array_optim , sdot_array ,
37
+ sdot_array_16 , sdot_array_16_sse
33
38
)
34
39
from td3a_cpp .tools import measure_time_dim
35
40
36
41
37
- def get_vectors (fct , n , h = 100 ):
38
- ctxs = [dict (va = numpy .random .randn (n ).astype (numpy . float64 ),
39
- vb = numpy .random .randn (n ).astype (numpy . float64 ),
42
+ def get_vectors (fct , n , h = 100 , dtype = numpy . float64 ):
43
+ ctxs = [dict (va = numpy .random .randn (n ).astype (dtype ),
44
+ vb = numpy .random .randn (n ).astype (dtype ),
40
45
dot = fct ,
41
46
x_name = n )
42
47
for n in range (10 , n , h )]
@@ -59,9 +64,9 @@ def get_vectors(fct, n, h=100):
59
64
# ++++++++++++++++++
60
65
#
61
66
62
- for fct in [dot_product , dot_cython_array ,
63
- dot_cython_array_optim , dot_array ,
64
- dot_array_16 , dot_array_16_sse ]:
67
+ for fct in [dot_product , ddot_cython_array ,
68
+ ddot_cython_array_optim , ddot_array ,
69
+ ddot_array_16 , ddot_array_16_sse ]:
65
70
ctxs = get_vectors (fct , 10000 if fct .__name__ != 'dot_product' else 1000 )
66
71
67
72
df = DataFrame (list (measure_time_dim ('dot(va, vb)' , ctxs , verbose = 1 )))
@@ -76,15 +81,48 @@ def get_vectors(fct, n, h=100):
76
81
cc = concat (dfs )
77
82
cc ['N' ] = cc ['x_name' ]
78
83
79
- fig , ax = plt .subplots (1 , 2 , figsize = (10 , 4 ))
80
- cc [cc .N <= 1100 ].pivot ('N' , 'fct' , 'average' ).plot (logy = True , ax = ax [0 ])
81
- cc [cc .fct != 'dot_product' ].pivot (
82
- 'N' , 'fct' , 'average' ).plot (logy = True , ax = ax [1 ])
83
- ax [0 ].set_title ("Comparison of cython dot implementations" )
84
- ax [1 ].set_title ("Comparison of cython dot implementations"
85
- "\n without dot_product" )
84
+ fig , ax = plt .subplots (2 , 2 , figsize = (10 , 10 ))
85
+ cc [cc .N <= 1100 ].pivot ('N' , 'fct' , 'average' ).plot (
86
+ logy = True , logx = True , ax = ax [0 , 0 ])
87
+ cc [cc .fct != 'dot_product' ].pivot ('N' , 'fct' , 'average' ).plot (
88
+ logy = True , ax = ax [0 , 1 ])
89
+ cc [cc .fct != 'dot_product' ].pivot ('N' , 'fct' , 'average' ).plot (
90
+ logy = True , logx = True , ax = ax [1 , 1 ])
91
+ ax [0 , 0 ].set_title ("Comparison of cython ddot implementations" )
92
+ ax [0 , 1 ].set_title ("Comparison of cython ddot implementations"
93
+ "\n without dot_product" )
86
94
87
95
###################
88
- # :epkg:`numpy` is cleary faster.
96
+ # :epkg:`numpy` is faster but we are able to catch up.
97
+
98
+ ###################################
99
+ # Same for floats
100
+ # +++++++++++++++
101
+ #
102
+ # Let's for single floats.
103
+
104
+ dfs = []
105
+ for fct in [numpy .dot , sdot_cython_array ,
106
+ sdot_cython_array_optim , sdot_array ,
107
+ sdot_array_16 , sdot_array_16_sse ]:
108
+ ctxs = get_vectors (fct , 10000 if fct .__name__ != 'dot_product' else 1000 ,
109
+ dtype = numpy .float32 )
110
+
111
+ df = DataFrame (list (measure_time_dim ('dot(va, vb)' , ctxs , verbose = 1 )))
112
+ df ['fct' ] = fct .__name__
113
+ dfs .append (df )
114
+ print (df .tail (n = 3 ))
115
+
116
+
117
+ cc = concat (dfs )
118
+ cc ['N' ] = cc ['x_name' ]
119
+
120
+ fig , ax = plt .subplots (1 , 2 , figsize = (10 , 4 ))
121
+ cc .pivot ('N' , 'fct' , 'average' ).plot (
122
+ logy = True , ax = ax [0 ])
123
+ cc .pivot ('N' , 'fct' , 'average' ).plot (
124
+ logy = True , logx = True , ax = ax [1 ])
125
+ ax [0 ].set_title ("Comparison of cython sdot implementations" )
126
+ ax [1 ].set_title ("Comparison of cython sdot implementations" )
89
127
90
128
plt .show ()
0 commit comments