• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            Performance of Iteration Methods (very good)

            I've been implementing numerical libraries in .NET and have come to some conclusions about iteration performance. My classes have to hold a large amount of data and be able to iterate through that data as quickly as possible. In order to compare various methods, I created a simple class called Data that encapsulates an array of doubles.

            Download source - 6 Kb

            Method #1: Enumeration

            Data implements IEnumerable. It contains GetEnumerator which returns its own DataEnumerator, an inner class.
            public IEnumerator GetEnumerator()
            ????{
            ???? returnnew DataEnumerator(this);
            ????}
            ????
            ????internalclass DataEnumerator : IEnumerator
            ????{
            ???? private Data internal_=null;
            ???? privateintindex=-1;

            ???? public DataEnumerator( Data data)
            ???? {
            ????????internal_=data;
            ???? }

            ???? publicobject Current
            ???? {
            ????????get
            ????????{
            ???????? returninternal_.Array[index];
            ????????}
            ???? }

            ???? publicbool MoveNext()
            ???? {
            ????????index++;
            ????????if(index >=internal_.Array.Length )
            ????????{
            ???????? returnfalse;
            ????????}
            ????????returntrue;
            ???? }

            ???? publicvoid Reset()
            ???? {
            ????????index=-1;
            ???? }
            ????}

            Method #2: Indexing

            I implemented an index operator on the class which simply calls the index operator on the array.

            ????publicdoublethis[intposition]
            ????{
            ???? get
            ???? {
            ????????returnarray_[position];
            ???? }
            ????}

            Method #3: Indirect Array

            I created a property to access the array.

            ????publicdouble[] Array
            ????{
            ???? get
            ???? {
            ????????returnarray_;
            ???? }
            ????}
            When iterating, I called the Array property and then its index operator.
            d = data.Array[j];

            Method #3: Direct Array

            I created a reference to the array.

            double[] array = data.Array;
            Then, I iterate through that reference.
            d = array[j];

            Method #4: Pointer Math

            Finally, I tried improving performance by iterating through the array in Managed C++ using pointer manipulation.

            ????????staticvoiditerate( Data&data)
            ????????{
            ???????? double d;
            ???????? double__pin*ptr=&(data.Array[0]);
            ???????? for(int i =0; i < data.Array.Length; i++)
            ???????? {
            ????????????d =*ptr;
            ????????????++ptr;
            ???????? }
            ????????}
            I called it this way:
            Pointer.iterate( data );

            Conclusions

            To test the different methods, I allocated 1,000,000 doubles into an array and indexed over all of them. I repeated this 1,000 times to minimize randomness. Here are the results...

            Results

            Enumeration is always slow. That's not surprising as I'm using a general data structure to hold the doubles. Each access performs a cast. The three operator/property methods differed very slightly. These are probably all optimized similarly. Using pointer math to traverse over the raw data was significantly faster. This is probably due to the fact that there's no bounds checking. In summary, if you have large amounts of data and performance is critical, consider using managed C++.

            超級(jí)collection類:

            using?System;
            using?System.Collections;

            namespace?Iterations
            {
            ??
            public?class?Data?:?IEnumerable
            ??
            {
            ????
            private?double[]?array_;

            ????
            public?Data(int?size)?
            ????
            {
            ??????array_?
            =?new?double[size];
            ??????Random?random?
            =?new?Random();
            ??????
            for?(?int?i?=?0;?i?<?size;?i++?)
            ??????
            {
            ????????array_[i]?
            =?random.Next();
            ??????}

            ????}


            ????
            public?double?this[int?position]
            ????
            {
            ??????
            get
            ??????
            {
            ????????
            return?array_[position];
            ??????}

            ????}


            ????
            public?double[]?Array
            ????
            {
            ??????
            get?
            ??????
            {
            ????????
            return?array_;
            ??????}

            ????}


            ????
            public?IEnumerator?GetEnumerator()?
            ????
            {
            ??????
            return?new?DataEnumerator(?this?);
            ????}

            ????
            ????
            internal?class?DataEnumerator?:?IEnumerator
            ????
            {
            ??????
            private?Data?internal_?=?null;
            ??????
            private?int?index?=?-1;

            ??????
            public?DataEnumerator(?Data?data?)?
            ??????
            {
            ????????internal_?
            =?data;
            ??????}


            ??????
            public?object?Current
            ??????
            {
            ????????
            get
            ????????
            {
            ??????????
            return?internal_.Array[index];
            ????????}

            ??????}


            ??????
            public?bool?MoveNext()
            ??????
            {
            ????????index
            ++;
            ????????
            if?(?index?>=?internal_.Array.Length?)?
            ????????
            {
            ??????????
            return?false;
            ????????}

            ????????
            return?true;
            ??????}


            ??????
            public?void?Reset()
            ??????
            {
            ????????index?
            =?-1;
            ??????}

            ????}

            ??}

            }
            test code:
            using?System;
            using?System.Collections;

            using?Iterations;

            namespace?Test
            {
            ??
            class?Test
            ??
            {
            ????
            private?double?d;
            ????
            private?int?reps;
            ????
            private?int?size;
            ????
            private?double?seconds;
            ????
            private?Data?data?=?null;
            ????
            private?DateTime?before;
            ????
            private?TimeSpan?total;

            ????[STAThread]
            ????
            static?void?Main(string[]?args)
            ????
            {
            ??????Test?test;
            ??????
            if?(?args.Length?>?1?)?
            ??????
            {
            ????????test?
            =?new?Test(?int.Parse(?args[0]?),?int.Parse(?args[1]?));
            ??????}
            ?
            ??????
            else?
            ??????
            {
            ????????test?
            =?new?Test();
            ??????}

            ??????test.TestAll();
            ????}


            ????
            public?Test()?:?this(?1000,?1000000?)
            ????
            {
            ????}


            ????
            public?Test(?int?reps,?int?size?)
            ????
            {
            ??????
            this.reps?=?reps;
            ??????
            this.size?=?size;
            ??????data?
            =?new?Data(?size?);
            ????}


            ????
            private?void?TestAll()
            ????
            {
            ??????System.Console.WriteLine();
            ??????System.Console.WriteLine(?
            "repetitions:?"?+?reps?);
            ??????System.Console.WriteLine(?
            "iterations:?"?+?size.ToString(?"e"?));
            ??????System.Console.WriteLine();
            ??????TestEnumeration();
            ??????TestIndexing();
            ??????TestIndirectArrays();
            ??????TestDirectArrays();
            ??????TestPointerMath();
            ??????System.Console.WriteLine();
            ????}


            ????
            private?void?TestEnumeration()?
            ????
            {
            ??????total?
            =?new?TimeSpan(?0?);
            ??????
            for?(?int?i?=?0;?i?<?reps;?i++?)?
            ??????
            {
            ????????before?
            =?DateTime.Now;
            ????????IEnumerator?enumerator?
            =?data.GetEnumerator();
            ????????
            while?(?enumerator.MoveNext()?)?
            ????????
            {
            ??????????d?
            =?(double)?enumerator.Current;
            ????????}

            ????????total?
            +=?DateTime.Now?-?before;
            ??????}

            ??????seconds?
            =?total.Seconds?+?(?total.Milliseconds?/?1000.0?);
            ??????System.Console.WriteLine(?
            "Enumeration:?\t\t"?+?seconds?+?"?seconds"?);
            ????}


            ????
            private?void?TestIndexing()
            ????
            {
            ??????total?
            =?new?TimeSpan(?0?);
            ??????
            for?(?int?i?=?0;?i?<?reps;?i++?)?
            ??????
            {
            ????????before?
            =?DateTime.Now;
            ????????
            for?(?int?j?=?0;?j?<?size;?j++?)
            ????????
            {
            ??????????d?
            =?data[j];
            ????????}

            ????????total?
            +=?DateTime.Now?-?before;
            ??????}

            ??????seconds?
            =?total.Seconds?+?(?total.Milliseconds?/?1000.0?);
            ??????System.Console.WriteLine(?
            "Indexing:?\t\t"?+?seconds?+?"?seconds"?);??????
            ????}


            ????
            private?void?TestIndirectArrays()
            ????
            {
            ??????total?
            =?new?TimeSpan(?0?);
            ??????
            for?(?int?i?=?0;?i?<?reps;?i++?)?
            ??????
            {
            ????????before?
            =?DateTime.Now;
            ????????
            for?(?int?j?=?0;?j?<?size;?j++?)
            ????????
            {
            ??????????d?
            =?data.Array[j];
            ????????}

            ????????total?
            +=?DateTime.Now?-?before;
            ??????}

            ??????seconds?
            =?total.Seconds?+?(?total.Milliseconds?/?1000.0?);
            ??????System.Console.WriteLine(?
            "Indirect?Arrays:?\t"?+?seconds?+?"?seconds"?);?
            ????}


            ????
            private?void?TestDirectArrays()
            ????
            {
            ??????total?
            =?new?TimeSpan(?0?);
            ??????
            for?(?int?i?=?0;?i?<?reps;?i++?)?
            ??????
            {
            ????????before?
            =?DateTime.Now;
            ????????
            double[]?array?=?data.Array;
            ????????
            for?(?int?j?=?0;?j?<?size;?j++?)
            ????????
            {
            ??????????d?
            =?array[j];
            ????????}

            ????????total?
            +=?DateTime.Now?-?before;
            ??????}

            ??????seconds?
            =?total.Seconds?+?(?total.Milliseconds?/?1000.0?);
            ??????System.Console.WriteLine(?
            "Direct?Arrays:?\t\t"?+?seconds?+?"?seconds"?);
            ????}


            ????
            private?void?TestPointerMath()
            ????
            {
            ??????total?
            =?new?TimeSpan(?0?);
            ??????
            for?(?int?i?=?0;?i?<?reps;?i++?)?
            ??????
            {
            ????????before?
            =?DateTime.Now;
            ????????Pointer.iterate(?data?);
            ????????total?
            +=?DateTime.Now?-?before;
            ??????}

            ??????seconds?
            =?total.Seconds?+?(?total.Milliseconds?/?1000.0?);
            ??????System.Console.WriteLine(?
            "Pointer?Math:?\t\t"?+?seconds?+?"?seconds"?);
            ????}

            ????}

            }

            posted on 2006-04-14 12:50 夢(mèng)在天涯 閱讀(352) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C#/.NET

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804303
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            www亚洲欲色成人久久精品| 久久天天躁狠狠躁夜夜躁2014| 综合网日日天干夜夜久久 | 一级做a爰片久久毛片看看| 久久久这里只有精品加勒比 | 国产精品18久久久久久vr| 国产成人AV综合久久| 久久国产精品无| 精品久久久久久久| 久久亚洲精品国产亚洲老地址| 久久人人爽人人爽人人爽| 欧美亚洲国产精品久久蜜芽| 色妞色综合久久夜夜| 成人精品一区二区久久久| 蜜桃麻豆WWW久久囤产精品| 一本久久a久久精品综合夜夜 | 亚洲国产欧美国产综合久久| 中文字幕成人精品久久不卡| 亚洲中文字幕无码久久精品1| 国产精品内射久久久久欢欢| 久久久亚洲欧洲日产国码aⅴ| 无码任你躁久久久久久| 日本免费久久久久久久网站| 日韩精品久久无码人妻中文字幕 | 久久午夜羞羞影院免费观看| 亚洲国产香蕉人人爽成AV片久久 | 99久久99久久精品国产片| 亚洲AV日韩精品久久久久久 | 久久国产精品成人片免费| 久久精品人妻中文系列| 欧美精品福利视频一区二区三区久久久精品 | 亚洲精品国产第一综合99久久| 91久久九九无码成人网站 | 热99RE久久精品这里都是精品免费| 久久精品国产精品青草| 国产∨亚洲V天堂无码久久久| 久久久亚洲欧洲日产国码二区| 久久综合亚洲色HEZYO社区| 久久人人爽人人爽人人片AV高清| 久久成人小视频| 亚洲中文字幕久久精品无码喷水|