Click here to Skip to main content
15,888,984 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
QuestionOn hold... Pin
Jam.X20-Mar-06 4:15
Jam.X20-Mar-06 4:15 
AnswerRe: On hold... Pin
Dave Kreskowiak20-Mar-06 10:11
mveDave Kreskowiak20-Mar-06 10:11 
GeneralRe: On hold... Pin
Jam.X20-Mar-06 13:36
Jam.X20-Mar-06 13:36 
QuestionInserting Page Break Pin
User 246299120-Mar-06 3:04
professionalUser 246299120-Mar-06 3:04 
AnswerRe: Inserting Page Break Pin
Robert Rohde20-Mar-06 7:20
Robert Rohde20-Mar-06 7:20 
GeneralRe: Inserting Page Break Pin
User 246299120-Mar-06 17:12
professionalUser 246299120-Mar-06 17:12 
QuestionAutoCad Tool Palette with .Net Pin
prakashdotc20-Mar-06 2:26
prakashdotc20-Mar-06 2:26 
QuestionIList vs array performance observation (.NET 2.0) Pin
wout de zeeuw19-Mar-06 22:52
wout de zeeuw19-Mar-06 22:52 
I was wanted to share some performance observations after fiddling around with arrays and IList<type>. Since e.g. a double[] a array automatically implements IList<double> in .NET 2.0 I can conveniently accept IList<double> as argument for any method without also needing an overload for double[]. However, after some quick testing it seems a performance penalty is involved using the IList interface! I used this small program to compare the options:

using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
<br />
namespace TestListVsArray {<br />
    class Program {<br />
        static void Main(string[] args) {<br />
            double[] a = new double[10000];<br />
<br />
            for (int i = 0; i < 3; i++) {<br />
                TestA(a);<br />
                TestB(a);<br />
                TestC(a);<br />
                TestD(a);<br />
                Console.WriteLine();<br />
            }<br />
        }<br />
<br />
        private static void TestA(IList<double> a) {<br />
            DateTime start = DateTime.Now;<br />
            double x = 0d;<br />
            for (int j = 0; j < 1000; j++) {<br />
                for (int i = 0; i < a.Count; i++) {<br />
                    x += a[i];<br />
                }<br />
            }<br />
            Console.WriteLine("{0}, {1}", DateTime.Now - start, x);<br />
        }<br />
<br />
        private static void TestB(IList<double> a) {<br />
            DateTime start = DateTime.Now;<br />
            double x = 0d;<br />
            int n = a.Count;<br />
            for (int j = 0; j < 1000; j++) {<br />
                for (int i = 0; i < n; i++) {<br />
                    x += a[i];<br />
                }<br />
            }<br />
            Console.WriteLine("{0}, {1}", DateTime.Now - start, x);<br />
        }<br />
<br />
        private static void TestC(double[] a) {<br />
            DateTime start = DateTime.Now;<br />
            double x = 0d;<br />
            for (int j = 0; j < 1000; j++) {<br />
                for (int i = 0; i < a.Length; i++) {<br />
                    x += a[i];<br />
                }<br />
            }<br />
            Console.WriteLine("{0}, {1}", DateTime.Now - start, x);<br />
        }<br />
<br />
        private static void TestD(double[] a) {<br />
            DateTime start = DateTime.Now;<br />
            double x = 0d;<br />
            int n = a.Length;<br />
            for (int j = 0; j < 1000; j++) {<br />
                for (int i = 0; i < n; i++) {<br />
                    x += a[i];<br />
                }<br />
            }<br />
            Console.WriteLine("{0}, {1}", DateTime.Now - start, x);<br />
        }<br />
    }<br />
}<br />

Output on my machine was (tests are repeated to rule out exceptional starting behaviour):
<br />
00:00:01.1015840, 0<br />
00:00:00.6008640, 0<br />
00:00:00.0801152, 0<br />
00:00:00.0801152, 0<br />
<br />
00:00:01.0915696, 0<br />
00:00:00.6008640, 0<br />
00:00:00.0801152, 0<br />
00:00:00.0801152, 0<br />
<br />
00:00:01.0815552, 0<br />
00:00:00.6108784, 0<br />
00:00:00.0801152, 0<br />
00:00:00.0801152, 0<br />

Concluding: iterating of the array interface is quite some faster than iterating over the IList interface of the same array object. Unfortunately using a.Count in the for loop also does cost some. I hoped the compiler/runtime was smart enough such that it would not cost anything.

Let's add those overloads again now. Sniff | :^)

Wout
AnswerRe: IList vs array performance observation (.NET 2.0) Pin
ricardojb20-Mar-06 9:29
ricardojb20-Mar-06 9:29 
GeneralRe: IList vs array performance observation (.NET 2.0) Pin
wout de zeeuw20-Mar-06 10:11
wout de zeeuw20-Mar-06 10:11 
GeneralRe: IList vs array performance observation (.NET 2.0) Pin
Ed.Poore24-Mar-06 8:01
Ed.Poore24-Mar-06 8:01 
GeneralRe: IList vs array performance observation (.NET 2.0) Pin
wout de zeeuw24-Mar-06 9:15
wout de zeeuw24-Mar-06 9:15 
GeneralRe: IList vs array performance observation (.NET 2.0) Pin
Ed.Poore24-Mar-06 10:57
Ed.Poore24-Mar-06 10:57 
QuestionIs this possible? Pin
Xaake19-Mar-06 10:48
Xaake19-Mar-06 10:48 
AnswerRe: Is this possible? Pin
Dave Kreskowiak19-Mar-06 12:36
mveDave Kreskowiak19-Mar-06 12:36 
GeneralRe: Is this possible? Pin
Xaake19-Mar-06 23:02
Xaake19-Mar-06 23:02 
QuestionExecuting code remotely Pin
Imtiaz Murtaza18-Mar-06 6:51
Imtiaz Murtaza18-Mar-06 6:51 
QuestionTransaction state in 3-tier architecture Pin
Sheshadrinath17-Mar-06 23:05
Sheshadrinath17-Mar-06 23:05 
AnswerRe: Transaction state in 3-tier architecture Pin
Guffa18-Mar-06 2:27
Guffa18-Mar-06 2:27 
QuestionReplace system tray clock Pin
bowlermonk17-Mar-06 7:33
bowlermonk17-Mar-06 7:33 
AnswerRe: Replace system tray clock Pin
Dave Kreskowiak17-Mar-06 9:20
mveDave Kreskowiak17-Mar-06 9:20 
GeneralRe: Replace system tray clock Pin
bowlermonk17-Mar-06 10:43
bowlermonk17-Mar-06 10:43 
GeneralRe: Replace system tray clock Pin
Dave Kreskowiak17-Mar-06 16:25
mveDave Kreskowiak17-Mar-06 16:25 
QuestionProblem getting thread to work Pin
rsw117-Mar-06 6:57
rsw117-Mar-06 6:57 
QuestionTextBox Control Pin
John L. DeVito17-Mar-06 6:17
professionalJohn L. DeVito17-Mar-06 6:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.