473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Generics question.The problem still exists.

I decide to make the problem clear from tip to toe.
my test code is from Anders Heilsberg's lecture on Tech-Ed 2004 on C#
Language enhancement. I slightly modified the code,not to the core part but
the display style.
My question is,when I first run the code,the performance of arraylist of
int is best,use only 30-50ms,other three do as expected. When I run the code
again,the arraylist of int has the worst performance,as it should be.I post
my code here,although this board is not convenient to display long code.I
tried my best to cleary layout the code.
Appreciate any help.

using System;
using System.Collections;
using System.Collections.Generic;

delegate void Method();

class Test
{
public string Name;
public Method Code;

public Test(string name,Method code)
{
Name = name;
Code = code;
}
}

class Program
{
static void RunTests(params Test[] tests)
{
for (int index = 0; index < tests.Length; index++) {
tests[index].Code();
}
for (int index = 0; index < tests.Length; index++)
{
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
tests[index].Code();
TimeSpan time = DateTime.Now - start;
DrawBar(index, time, tests[index].Name);
}
}

static void DrawBar(int index, TimeSpan time, string text)
{
text = " " + text;
Console.WriteLine(text);
ConsoleColor bg = Console.BackgroundColor;
ConsoleColor fg = Console.ForegroundColor;
Console.BackgroundColor = (index & 2) == 0 ? ConsoleColor.DarkBlue :
ConsoleColor.DarkRed;
Console.ForegroundColor = ConsoleColor.White;
int size = (int)(time.Milliseconds/10);
if (size > 79) size = 79;
text = " " + time.Milliseconds.ToString() + "ms";
for (int i = 0; i < size; i++)
Console.Write(i < text.Length ? text[i] : ' ');
Console.WriteLine();
for (int i = 0; i < size; i++) Console.Write(' ');
Console.WriteLine();
Console.WriteLine();
Console.BackgroundColor = bg;
Console.ForegroundColor = fg;
}

static void Main(string[] args)
{
Console.WriteLine();
RunTests(
new Test("ArrayList of string",
delegate {
ArrayList list = new ArrayList();
for (int i = 0; i < 10000; i++)
{
list.Add("hello");
string s = (string)list[0];
list.RemoveAt(0);
}
}),
new Test("ArrayList of int",
delegate {
ArrayList list = new ArrayList();
for (int i = 0; i < 10000; i++)
{
list.Add(123);
int x = (int)list[0];
list.RemoveAt(0);
}

}),
new Test("List<string>",
delegate {
List<string> list = new List<string>();
for (int i = 0; i < 10000; i++)
{
list.Add("hello");
string s = list[0];
list.RemoveAt(0);
}

}),
new Test("List<int>",
delegate {
List<int> list = new List<int>();
for (int i = 0; i < 10000; i++)
{
list.Add(123);
int x = list[0];
list.RemoveAt(0);
}

})
);
Console.ReadLine();
}
}

Feb 24 '06 #1
0 1142

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Fred | last post by:
Is there a way to use generics with vs.net 2003?? I was searching around and I saw gyro but does it works with vs.net 2003?? if it does how am I suppouse to install it. If it doesnt is there...
2
by: Wiktor Zychla | last post by:
I've read several documents about upcoming C# generics and I still cannot understand one thing. Would it be valid to write a code like this: class SomeClass { public void AMethod<T>(T a, T...
27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
2
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
7
by: Gene Vital | last post by:
Hi all, I need some help in understanding how to use Generics. I have a class based on a user control that can be put on any Container at runtime, I want to be able to call a method on the...
1
by: Peter Kirk | last post by:
Hi I have never used generics before, and I was wondering if the following sort of use was acceptable/normal for a method: public IList<IPerson> GetPersons() { IList<IPerson> personList =...
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
3
by: Showjumper | last post by:
Back in asp.net 1.1 i made custom collection classes per Karl Seguin's article On the Way to Mastering ASP.NET: Introducing Custom Entity Classes to take advantage of strongly typed data. Now with...
2
by: Martin Robins | last post by:
Marc has sorted my problem with the collections, however I am now in a new scenario; I am trying to compare a generic property value before setting it as shown below ... public T Value { get {...
4
by: wizard of oz | last post by:
I'm trying to write a data class using generics. This class is a Sparse Matrix meaning that it can have many dimensions but not many entries (e.g. a 100 by 100 matrix, but there might only be 10...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.