[Mono-list] offtopic, but cool

Cory Nelson phrosty@gmail.com
Wed, 12 May 2004 07:56:29 -0700


The only difference is the ArrayList requires boxing to Object.

ArrayList:
L_002a: newobj instance void
[System.Drawing]System.Drawing.Rectangle::.ctor(int32, int32, int32,
int32)
L_002f: box [System.Drawing]System.Drawing.Rectangle
L_0034: callvirt instance int32
[mscorlib]System.Collections.ArrayList::Add(object)

List<Rectangle>:
L_008c: newobj instance void
[System.Drawing]System.Drawing.Rectangle::.ctor(int32, int32, int32,
int32)
L_0091: callvirt instance int32
[mscorlib]System.Collections.Generic.List!1<[System.Drawing]System.Drawing.=
Rectangle>::Add(!0)

On Wed, 12 May 2004 10:18:57 -0400, Dan <dan@astusa.com> wrote:
>=20
> Cory, I'm curious, did you compare the IL to see whats different?
>=20
> -Dan
>=20
>=20
> -----Original Message-----
> From: mono-list-admin@lists.ximian.com
> [mailto:mono-list-admin@lists.ximian.com] On Behalf Of Lluis Sanchez
> Sent: Wednesday, May 12, 2004 9:17 AM
> To: Cesar Mello
> Cc: Cory Nelson; mono-list
> Subject: Re: [Mono-list] offtopic, but cool
>=20
> System.Drawing.Rectangle is a struct, so it is copied by value, not by
> reference.
>=20
> On dc, 2004-05-12 at 14:41, Cesar Mello wrote:
> > Hi Cory,
> >
> > In the C++ sample, you are passing the rect by value, so you are
> > saving a copy in the list. In C# a reference to the object is used, so
> > there is no copy-construction overhead.
> >
> > You can change the list<rect> to a list<rect*>, but this way you have
> > to manage the memory by yourlself.
> >
> > []
> > Mello
> >
> >
> >
> > Cory Nelson wrote:
> > > Just got done installing the VS.NET 2005 preview and did a small test=
.
> > >
> > > I compared an ArrayList of Rectangles to a List<Rectangle>, and
> > > timed inserting 1mil rects into each.  I also wrote an equivalent c++
> app.
> > > Got some interesting results:
> > >
> > > ArrayList: 265ms
> > > List<Rectangle>: 62ms
> > > list<rect>: 141ms
> > >
> > > So it seems with generics .NET is finally faster than c++ (at least,
> > > in this case).
> > >
> > > Esta mensagem foi verificada pelo E-mail Protegido Terra.
> > > Scan engine: VirusScan / Atualizado em 10/05/2004 / Vers=E3o: 1.5.2
> > > Proteja o seu e-mail Terra: http://www.emailprotegido.terra.com.br/
> > >
> > >
> > > ____________________________________________________________________
> > > #region Using directives
> > >
> > > using System;
> > > using System.Collections;
> > > using System.Collections.Generic;
> > > using System.Drawing;
> > >
> > > #endregion
> > >
> > > namespace SpeedTest {
> > >     class Program {
> > >             static void Main(string[] args) {
> > >                     ArrayList al =3D new ArrayList();
> > >                     List<Rectangle> rl =3D new List<Rectangle>();
> > >                     DateTime start, end;
> > >
> > >                     GC.Collect();
> > >                     GC.WaitForPendingFinalizers();
> > >
> > >                     start =3D DateTime.Now;
> > >                     for (int i =3D 0; i < 1000000; i++)
> > >                             al.Add(new Rectangle(i, i, i, i));
> > >                     end =3D DateTime.Now;
> > >
> > >                     Console.WriteLine("Arraylist:       {0:F3}ms",
> (end-start).TotalMilliseconds);
> > >
> > >                     GC.Collect();
> > >                     GC.WaitForPendingFinalizers();
> > >
> > >                     start =3D DateTime.Now;
> > >                     for (int i =3D 0; i < 1000000; i++)
> > >                             rl.Add(new Rectangle(i, i, i, i));
> > >                     end =3D DateTime.Now;
> > >
> > >                     Console.WriteLine("List<Rectangle>:  {0:F3}ms", (=
end
> - start).TotalMilliseconds);
> > >             }
> > >     }
> > > }
> > >
> > >
> > >
> > > ____________________________________________________________________
> > > #include <list>
> > > #include <iostream>
> > > #include <ctime>
> > > using namespace std;
> > >
> > > struct rect {
> > >     int x;
> > >     int y;
> > >     int width;
> > >     int height;
> > > };
> > >
> > > int main(void) {
> > >     list<rect> rl;
> > >
> > >     clock_t start=3Dclock();
> > >     for(int i=3D0; i<1000000; i++) {
> > >             rect r=3D{i, i, i, i};
> > >             rl.push_back(r);
> > >     }
> > >     clock_t end=3Dclock();
> > >
> > >     cout << "list<rect>: " <<
> > > (((float)(end-start))/((float)CLOCKS_PER_SEC)*1000.0f) << "ms" <<
> > > endl;
> > >
> > >     return 0;
> > > }
> > >
> > >
> >
>=20
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>=20
>