constructor constraint in C# 2.0

O

Ole Andre Karlson

Hi

Im using Whidbey version 8.0.30703.27, working with C# 2.0.

I discovered that creating an object through the
constructor constraint is a sloooow process... Is this
just because I'm working on a beta version, or is there a
deeper cause for this?

My time measurements for creating 1 million objects is 6.7
seconds using the new constraint versus 0.2 seconds using
an abstract factory....

never mind the other strange constraint in the example, it
shouldn't make a difference :)

----- new constrint ------
abstract class A<FB> where FB : A<FB>, new()
{
int number = 1000000;
FB[] myArray;
public void generate()
{
myArray = new FB[number];
for (int i = 0; i < number; i++)
{
myArray = new FB();
}
}
}

class B : A<B>
{
}

class Control
{
public static void Main()
{
B b = new B();
DateTime start = DateTime.Now;
b.generate();
DateTime stop = DateTime.Now;
TimeSpan res = stop.Subtract(start);
Console.WriteLine(res);
}
}

------- abstract factory ---------
abstract class A<FB> where FB: A<FB>
{
abstract public A_Fact<FB> fact();
int number = 1000000;
FB[] myArray;
public void generate()
{
myArray = new FB[number];
for (int i = 0; i < number; i++)
{
myArray = fact().newA();
}
}
}

abstract class A_Fact<FB> where FB : A<FB>
{
abstract public FB newA();
}

class B_Fact : A_Fact<B>
{
public static B_Fact f = new B_Fact();
public override B newA() { return new B(); }
}

class B : A<B>
{
public override A_Fact<B> fact()
{
return B_Fact.f;
}
}

class Control
{
public static void Main()
{
B b = new B();
DateTime start = DateTime.Now;
b.generate();
DateTime stop = DateTime.Now;
TimeSpan res = stop.Subtract(start);
Console.WriteLine(res);
}
}
 
W

Willy Denoyette [MVP]

Hmmm... version 8.0.30703.27 is not the latest Beta1.

Anyway, I'm sure you would get better answers when posting whidbey questions
to the whidbey generics NG' - microsoft.private.whidbey.generics

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top