PC Review


Reply
Thread Tools Rate Thread

Code Performance Question

 
 
MattC
Guest
Posts: n/a
 
      6th Jan 2005
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


 
Reply With Quote
 
 
 
 
Nick Malik [Microsoft]
Guest
Posts: n/a
 
      10th Jan 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      10th Jan 2005
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 [Microsoft]" <(E-Mail Removed)> wrote in message
news:QOSdne7gdb2DPH_cRVn-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
MattC
Guest
Posts: n/a
 
      11th Jan 2005
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 [Microsoft]" <(E-Mail Removed)> wrote in message
news:QOSdne7gdb2DPH_cRVn-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
vba Code Performance Klips Microsoft Excel Programming 1 13th Apr 2007 12:27 PM
Trying to improve the performance of my code =?Utf-8?B?Q2FybG8=?= Microsoft Excel Programming 2 22nd Dec 2006 09:06 AM
timing performance of code ronny Microsoft VC .NET 6 28th Feb 2005 02:13 PM
performance and code behind =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= Microsoft ASP .NET 2 8th Feb 2005 02:40 PM
Code Performance koorb Microsoft VB .NET 18 24th Aug 2004 10:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:18 AM.