{"id":569,"date":"2009-10-03T22:11:37","date_gmt":"2009-10-03T20:11:37","guid":{"rendered":"http:\/\/dev.bratched.fr\/fr\/manipulations-de-matrices-avec-du-code-non-securise\/"},"modified":"2009-10-03T22:11:37","modified_gmt":"2009-10-03T20:11:37","slug":"manipulations-de-matrices-avec-du-code-non-securise","status":"publish","type":"post","link":"https:\/\/bratched.com\/fr\/2009\/10\/03\/manipulations-de-matrices-avec-du-code-non-securise\/","title":{"rendered":"Manipulations de matrices avec du code non s\u00e9curis\u00e9"},"content":{"rendered":"<h1>La classe Matrix<\/h1>\n<p>Dans cet article nous allons comparer 2 m\u00e9thodes qui traitent de multiplication de matrices en .Net | C#.<\/p>\n<p>Nous commen\u00e7ons par la d\u00e9finition de la classe matrice :\u00a0 Matrix<\/p>\n<div id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c5893f13-7290-415d-a8ac-e6282530d67b\" class=\"wlWriterSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<pre>    class Matrix\n    {\n        private readonly double[,] _matrix;\n        public Matrix(int dim1, int dim2)\n        {\n            _matrix = new double[dim1, dim2];\n        }\n\n        public int Height { get { return _matrix.GetLength(0); } }\n        public int Width { get { return _matrix.GetLength(1); } }\n\n        public double this[int x, int y]\n        {\n            get { return _matrix[x, y]; }\n            set { _matrix[x, y] = value; }\n        }\n    }\n<\/pre>\n<\/div>\n<h1>Multiplications de matrices<\/h1>\n<p>Voici le premier algorithme de multiplication de matrice qui r\u00e9alise l\u2019op\u00e9ration de mani\u00e8re standard.<\/p>\n<div id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b52b8b7c-a758-40a2-ab3e-12b1a413665d\" class=\"wlWriterSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<pre>    public static Matrix NaiveMultiplication(Matrix m1, Matrix m2)\n    {\n        Matrix resultMatrix = new Matrix(m1.Height, m2.Width);\n        for (int i = 0; i &lt; resultMatrix.Height; i++)\n        {\n            for (int j = 0; j &lt; resultMatrix.Width; j++)\n            {\n                resultMatrix[i, j] = 0;\n                for (int k = 0; k &lt; m1.Width; k++)\n                {\n                    resultMatrix[i, j] += m1[i, k] * m2[k, j];\n                }\n            }\n        }\n        return resultMatrix;\n    }\n<\/pre>\n<\/div>\n<p>Voici le second algorithme qui r\u00e9alise la m\u00eame chose, mais cette fois en utilisant du code non s\u00e9curis\u00e9 (unsafe)<\/p>\n<p><!--more--><\/p>\n<div id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:737b3d23-f311-4bc6-bc4c-8c7f32ba7bc4\" class=\"wlWriterSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<pre>   public unsafe static Matrix UnsafeMultiplication(Matrix m1, Matrix m2)\n   {\n       int h = m1.Height;\n       int w = m2.Width;\n       int l = m1.Width;\n       Matrix resultMatrix = new Matrix(h, w);\n       unsafe\n       {\n           fixed (double* pm = resultMatrix._matrix, pm1 = m1._matrix, pm2 = m2._matrix)\n           {\n               int i1, i2;\n               for (int i = 0; i &lt; h; i++)\n               {\n                   i1 = i * l;\n                   for (int j = 0; j &lt; w; j++)\n                   {\n                       i2 = j;\n                       double res = 0;\n                       for (int k = 0; k &lt; l; k++, i2 += w)\n                       {\n                           res += pm1[i1 + k] * pm2[i2];\n                       }\n                       pm[i * w + j] = res;\n                   }\n               }\n           }\n       }\n       return resultMatrix;\n   }<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<h1>Mesure des performances<\/h1>\n<p>Il est temps maintenant de comparer les performances des 2 op\u00e9rations<\/p>\n<h2>Programme de test de performance<\/h2>\n<div id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8b794377-880e-4063-b5bc-ebd7b88e413e\" class=\"wlWriterSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<pre>    class Program\n    {\n        [DllImport(\"kernel32.dll\")]\n        static extern void QueryPerformanceCounter(ref long ticks);\n\n        static long Measure(Action action, int count)\n        {\n            long startTicks = 0;\n            QueryPerformanceCounter(ref startTicks);\n            for (int i = 0; i &lt; count; i++)\n            {\n                action();\n            }\n            long endTicks = 0;\n            QueryPerformanceCounter(ref endTicks);\n            return endTicks - startTicks;\n        }\n\n        static void Main(string[] args)\n        {\n            Random random = new Random();\n\n            Matrix m1 = new Matrix(20, 30);\n            for (int i = 0; i &lt; m1.Height; i++)\n            {\n                for (int j = 0; j &lt; m1.Width; j++)\n                {\n                    m1[i, j] = random.Next(-100, 100);\n                }\n            }\n\n            Matrix m2 = new Matrix(30, 40);\n            for (int i = 0; i &lt; m2.Height; i++)\n            {\n                for (int j = 0; j &lt; m2.Width; j++)\n                {\n                    m2[i, j] = random.Next(-100, 100);\n                }\n            }\n\n            Console.WriteLine(Measure(() =&gt; Matrix.NaiveMultiplication(m1, m2), 10000));\n            Console.WriteLine(Measure(() =&gt; Matrix.UnsafeMultiplication(m1, m2), 10000));\n        }\n    }<\/pre>\n<\/div>\n<p>Dans ce programme de test nous r\u00e9alisons 10000 multiplications de 2 matrices g\u00e9n\u00e9r\u00e9es de fa\u00e7on al\u00e9atoires avec des tailles de 20&#215;30 et 30&#215;40 \u00e9l\u00e9ments.<\/p>\n<h2>R\u00e9sultat du test de performance<\/h2>\n<table border=\"1\" width=\"400\" cellspacing=\"0\" cellpadding=\"2\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"200\"><strong>M\u00e9thode<\/strong><\/td>\n<td valign=\"top\" width=\"198\"><strong>CPU cycles<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"200\">multiplication unsafe<\/td>\n<td valign=\"top\" width=\"198\">4485698<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"200\">multiplication traditionnelle<\/td>\n<td valign=\"top\" width=\"198\">58762273<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Les r\u00e9sultats montrent que la multiplication de matrice r\u00e9alis\u00e9e de fa\u00e7on traditionnelle est plus lente d\u2019un facteur de 13 compar\u00e9e \u00e0 la multiplication r\u00e9alis\u00e9e par le code non s\u00e9curis\u00e9 et travaillant directement avec la m\u00e9moire.<\/p>\n<div class=\"clearfix\"><\/div>\n<div class=\"tag\">\n<p>Tags:<\/p>\n<ul>\n<li><a title=\"dotnet\" href=\"\/fr\/component\/tag\/dotnet.html\" rel=\"tag\">dotnet<\/a><\/li>\n<li><a title=\"unsafe\" href=\"\/fr\/component\/tag\/unsafe.html\" rel=\"tag\">unsafe<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>La classe Matrix Dans cet article nous allons comparer 2 m\u00e9thodes qui traitent de multiplication de matrices en .Net | C#. Nous commen\u00e7ons par la d\u00e9finition de la classe matrice :\u00a0 Matrix class Matrix { private readonly double[,] _matrix; public Matrix(int dim1, int dim2) { _matrix = new double[dim1, dim2]; } public int Height { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[18,44,8],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/bratched.com\/fr\/wp-json\/wp\/v2\/posts\/569"}],"collection":[{"href":"https:\/\/bratched.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bratched.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bratched.com\/fr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bratched.com\/fr\/wp-json\/wp\/v2\/comments?post=569"}],"version-history":[{"count":0,"href":"https:\/\/bratched.com\/fr\/wp-json\/wp\/v2\/posts\/569\/revisions"}],"wp:attachment":[{"href":"https:\/\/bratched.com\/fr\/wp-json\/wp\/v2\/media?parent=569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bratched.com\/fr\/wp-json\/wp\/v2\/categories?post=569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bratched.com\/fr\/wp-json\/wp\/v2\/tags?post=569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}