[Mono-list] offtopic, but cool

Cesar Mello cmello2@terra.com.br
Wed, 12 May 2004 09:41:24 -0300


This is a multi-part message in MIME format.
--------------070708050907030807090107
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

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;
>}
>
>  
>


--------------070708050907030807090107
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Hi Cory,<br>
<br>
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.<br>
<br>
You can change the list&lt;rect&gt; to a list&lt;rect*&gt;, but this
way you have to manage the memory by yourlself. <br>
<br>
[]<br>
Mello<br>
<br>
<br>
<br>
Cory Nelson wrote:<br>
<blockquote cite="mid9b1d0614040512045626083aff@mail.gmail.com"
 type="cite">
  <pre wrap="">Just got done installing the VS.NET 2005 preview and did a small test.

I compared an ArrayList of Rectangles to a List&lt;Rectangle&gt;, and timed
inserting 1mil rects into each.  I also wrote an equivalent c++ app. 
Got some interesting results:

ArrayList: 265ms
List&lt;Rectangle&gt;: 62ms
list&lt;rect&gt;: 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&atilde;o: 1.5.2
Proteja o seu e-mail Terra: <a class="moz-txt-link-freetext" href="http://www.emailprotegido.terra.com.br/">http://www.emailprotegido.terra.com.br/</a>
  </pre>
  <pre wrap="">
<hr width="90%" size="4">
#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&lt;Rectangle&gt; rl = new List&lt;Rectangle&gt;();
			DateTime start, end;

			GC.Collect();
			GC.WaitForPendingFinalizers();

			start = DateTime.Now;
			for (int i = 0; i &lt; 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 &lt; 1000000; i++)
				rl.Add(new Rectangle(i, i, i, i));
			end = DateTime.Now;

			Console.WriteLine("List&lt;Rectangle&gt;:  {0:F3}ms", (end - start).TotalMilliseconds);
		}
	}
}

  </pre>
  <pre wrap="">
<hr width="90%" size="4">
#include &lt;list&gt;
#include &lt;iostream&gt;
#include &lt;ctime&gt;
using namespace std;

struct rect {
	int x;
	int y;
	int width;
	int height;
};

int main(void) {
	list&lt;rect&gt; rl;

	clock_t start=clock();
	for(int i=0; i&lt;1000000; i++) {
		rect r={i, i, i, i};
		rl.push_back(r);
	}
	clock_t end=clock();

	cout &lt;&lt; "list&lt;rect&gt;: " &lt;&lt; (((float)(end-start))/((float)CLOCKS_PER_SEC)*1000.0f) &lt;&lt; "ms" &lt;&lt; endl;

	return 0;
}

  </pre>
</blockquote>
<br>
</body>
</html>

--------------070708050907030807090107--