[Mono-list] offtopic, but cool
Lluis Sanchez
lluis@ximian.com
Wed, 12 May 2004 15:17:09 +0200
System.Drawing.Rectangle is a struct, so it is copied by value, not by
reference.
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ão: 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 = new ArrayList();
> > List<Rectangle> rl = new List<Rectangle>();
> > DateTime start, end;
> >
> > GC.Collect();
> > GC.WaitForPendingFinalizers();
> >
> > start = DateTime.Now;
> > for (int i = 0; i < 1000000; i++)
> > al.Add(new Rectangle(i, i, i, i));
> > end = DateTime.Now;
> >
> > Console.WriteLine("Arraylist: {0:F3}ms", (end-start).TotalMilliseconds);
> >
> > GC.Collect();
> > GC.WaitForPendingFinalizers();
> >
> > start = DateTime.Now;
> > for (int i = 0; i < 1000000; i++)
> > rl.Add(new Rectangle(i, i, i, i));
> > end = 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=clock();
> > for(int i=0; i<1000000; i++) {
> > rect r={i, i, i, i};
> > rl.push_back(r);
> > }
> > clock_t end=clock();
> >
> > cout << "list<rect>: " << (((float)(end-start))/((float)CLOCKS_PER_SEC)*1000.0f) << "ms" << endl;
> >
> > return 0;
> > }
> >
> >
>