Code Performance Question

M

MattC

Hi,

I have a collection class derived from ArrayList, it stores colletions of
objects from my objet model. I Didn't want to have to create a specialised
collection for each object type so I created the following static method.

public static MyCollection CreateGenericCollection(DataTable rates, Assembly
a, Type t)
{
MyCollection c = new MyCollection(); // create new collection

Type[] types = new Type[1];
types[0] = typeof(DataRow); //all my BLL object have to take a DataRow in
a constructor

object obj=Activator.CreateInstance(t); //get the type of the object I
am creating collection of
ConstructorInfo ci = t.GetConstructor(types); //get its constructor info
object[] p=new object[1];

foreach(DataRow row in rates.Rows) //convert to for loop later
{
p[0] = row;
c.Add(ci.Invoke(p)); //invoke the constructor passing in the row and add
to Arraylist
}

return c; //send back filled collection when done?
}

Oringinally I had each BLL object to something like this.

public static MyCollection CreateLabourRateCollection(DataTable rates)
{
MyCollection c = new MyCollection();

foreach(DataRow row in rates.Rows)
{
c.Add(new LabourRate(row));
}

return c;
}

Ok my question.

How slow is Reflection given that the Assemblies to lookup are all located
in the same place?

Also any suggestions on the method would be cool, thanks.

TIA

MattC
 
N

Nick Malik [Microsoft]

You went to a lot of work to refactor away from simple, readable code to
something much more complicated and much less readable. Were there bugs in
the original code?

Maybe I'm just a bit more jaded than you are. When I see a need for
something that I've done a dozen times before, I either use a strategy
pattern or simply encapsulation. Not dissimiliar to your original code.
Reflection, as part of fundamental code that is Likely to run in a loop,
just seems like a convenience that is ill afforded.

By now, I'm sure that you know that Generics are coming in the next version
of .Net. You appear too advanced to have missed that bit of news.

So, in the current code, you could be coding your classes so that they are
easily replaced with generic classes when they become available. Or you
could code for readability, so that the person who gets to maintain this
code can easily resolve defects.

This code does neither. It is clever, true. But I see no value over the
previous incarnation.

Sorry if this isn't what you wanted to hear.

As for performance, only testing will tell. I cannot imagine your code will
perform as well after this change. If it is still of "acceptable" speed
will depend on your app.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
MattC said:
Hi,

I have a collection class derived from ArrayList, it stores colletions of
objects from my objet model. I Didn't want to have to create a specialised
collection for each object type so I created the following static method.

public static MyCollection CreateGenericCollection(DataTable rates, Assembly
a, Type t)
{
MyCollection c = new MyCollection(); // create new collection

Type[] types = new Type[1];
types[0] = typeof(DataRow); //all my BLL object have to take a DataRow in
a constructor

object obj=Activator.CreateInstance(t); //get the type of the object I
am creating collection of
ConstructorInfo ci = t.GetConstructor(types); //get its constructor info
object[] p=new object[1];

foreach(DataRow row in rates.Rows) //convert to for loop later
{
p[0] = row;
c.Add(ci.Invoke(p)); //invoke the constructor passing in the row and add
to Arraylist
}

return c; //send back filled collection when done?
}

Oringinally I had each BLL object to something like this.

public static MyCollection CreateLabourRateCollection(DataTable rates)
{
MyCollection c = new MyCollection();

foreach(DataRow row in rates.Rows)
{
c.Add(new LabourRate(row));
}

return c;
}

Ok my question.

How slow is Reflection given that the Assemblies to lookup are all located
in the same place?

Also any suggestions on the method would be cool, thanks.

TIA

MattC
 
M

Mark

Interesting. Could you give a quick example of how one "could be coding
your classes so that they are easily replaced with generic classes when they
become available."

Thanks in advance.

mark

Nick Malik said:
You went to a lot of work to refactor away from simple, readable code to
something much more complicated and much less readable. Were there bugs in
the original code?

Maybe I'm just a bit more jaded than you are. When I see a need for
something that I've done a dozen times before, I either use a strategy
pattern or simply encapsulation. Not dissimiliar to your original code.
Reflection, as part of fundamental code that is Likely to run in a loop,
just seems like a convenience that is ill afforded.

By now, I'm sure that you know that Generics are coming in the next version
of .Net. You appear too advanced to have missed that bit of news.

So, in the current code, you could be coding your classes so that they are
easily replaced with generic classes when they become available. Or you
could code for readability, so that the person who gets to maintain this
code can easily resolve defects.

This code does neither. It is clever, true. But I see no value over the
previous incarnation.

Sorry if this isn't what you wanted to hear.

As for performance, only testing will tell. I cannot imagine your code will
perform as well after this change. If it is still of "acceptable" speed
will depend on your app.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
MattC said:
Hi,

I have a collection class derived from ArrayList, it stores colletions of
objects from my objet model. I Didn't want to have to create a specialised
collection for each object type so I created the following static method.

public static MyCollection CreateGenericCollection(DataTable rates, Assembly
a, Type t)
{
MyCollection c = new MyCollection(); // create new collection

Type[] types = new Type[1];
types[0] = typeof(DataRow); //all my BLL object have to take a
DataRow
in
a constructor

object obj=Activator.CreateInstance(t); //get the type of the object I
am creating collection of
ConstructorInfo ci = t.GetConstructor(types); //get its constructor info
object[] p=new object[1];

foreach(DataRow row in rates.Rows) //convert to for loop later
{
p[0] = row;
c.Add(ci.Invoke(p)); //invoke the constructor passing in the row and add
to Arraylist
}

return c; //send back filled collection when done?
}

Oringinally I had each BLL object to something like this.

public static MyCollection CreateLabourRateCollection(DataTable rates)
{
MyCollection c = new MyCollection();

foreach(DataRow row in rates.Rows)
{
c.Add(new LabourRate(row));
}

return c;
}

Ok my question.

How slow is Reflection given that the Assemblies to lookup are all located
in the same place?

Also any suggestions on the method would be cool, thanks.

TIA

MattC
 
M

MattC

Nick,

Actually, yes I have missed talk about Generics (although I will now
investigate). Unfortunately this project will not be using the next
version. My refactoring was out of a need to reduce unwanted derivation
from the base class where it is not neccesary. I agree the orginal is
simpler but when it is implemented in 15-20 derived classes the refactoring
(which only took a a few MSDN examples to create) seemed worth it for a
simpler design.

As this project is new and very very suseptable to feature creeping by the
users I try to keep things as generic as possible so that when the
enevitable enmasse change comes along I have but one or two classes to alter
and retest as opposed to many.

Call me jaded byexpecting the worse, I just call it "bl**dy users" :)

As for speed, as you say I guess usage will tell.

Thanks for your time

MattC
Nick Malik said:
You went to a lot of work to refactor away from simple, readable code to
something much more complicated and much less readable. Were there bugs
in
the original code?

Maybe I'm just a bit more jaded than you are. When I see a need for
something that I've done a dozen times before, I either use a strategy
pattern or simply encapsulation. Not dissimiliar to your original code.
Reflection, as part of fundamental code that is Likely to run in a loop,
just seems like a convenience that is ill afforded.

By now, I'm sure that you know that Generics are coming in the next
version
of .Net. You appear too advanced to have missed that bit of news.

So, in the current code, you could be coding your classes so that they are
easily replaced with generic classes when they become available. Or you
could code for readability, so that the person who gets to maintain this
code can easily resolve defects.

This code does neither. It is clever, true. But I see no value over the
previous incarnation.

Sorry if this isn't what you wanted to hear.

As for performance, only testing will tell. I cannot imagine your code
will
perform as well after this change. If it is still of "acceptable" speed
will depend on your app.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
MattC said:
Hi,

I have a collection class derived from ArrayList, it stores colletions of
objects from my objet model. I Didn't want to have to create a specialised
collection for each object type so I created the following static method.

public static MyCollection CreateGenericCollection(DataTable rates, Assembly
a, Type t)
{
MyCollection c = new MyCollection(); // create new collection

Type[] types = new Type[1];
types[0] = typeof(DataRow); //all my BLL object have to take a DataRow in
a constructor

object obj=Activator.CreateInstance(t); //get the type of the object
I
am creating collection of
ConstructorInfo ci = t.GetConstructor(types); //get its constructor info
object[] p=new object[1];

foreach(DataRow row in rates.Rows) //convert to for loop later
{
p[0] = row;
c.Add(ci.Invoke(p)); //invoke the constructor passing in the row and add
to Arraylist
}

return c; //send back filled collection when done?
}

Oringinally I had each BLL object to something like this.

public static MyCollection CreateLabourRateCollection(DataTable rates)
{
MyCollection c = new MyCollection();

foreach(DataRow row in rates.Rows)
{
c.Add(new LabourRate(row));
}

return c;
}

Ok my question.

How slow is Reflection given that the Assemblies to lookup are all
located
in the same place?

Also any suggestions on the method would be cool, thanks.

TIA

MattC
 

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