473,385 Members | 1,673 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,385 software developers and data experts.

Code Performance Question

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
Nov 16 '05 #1
3 1345
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" <m@m.com> wrote in message
news:Or**************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #2
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]" <ni*******@hotmail.nospam.com> wrote in message
news:QO********************@comcast.com...
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" <m@m.com> wrote in message
news:Or**************@TK2MSFTNGP10.phx.gbl...
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


Nov 16 '05 #3
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]" <ni*******@hotmail.nospam.com> wrote in message
news:QO********************@comcast.com...
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" <m@m.com> wrote in message
news:Or**************@TK2MSFTNGP10.phx.gbl...
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


Nov 16 '05 #4

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

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
7
by: Randell D. | last post by:
Folks, I have a Javascript performance question that I might have problems explaining... In PHP, better performance can be obtained dealing directly with a variable, as opposed to an element...
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
4
by: David Sworder | last post by:
Hi there, I come from a Visual C++ background. When writing a service that's exposed to the Internet, I had to check the incoming data stream (from the client) VERY carefully. If a hacker was...
15
by: Craig Wagner | last post by:
I have a situation where I have a series of character codes stored in the database. In some cases they are the ASCII value of the character, in other cases they are the > 127 character code value...
88
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
18
by: Rune B | last post by:
Hi Group I was considering using a Generic Dictionary<> as a value container inside my business objects, for the reason of keeping track of fields changed or added and so on. - But how...
239
by: Eigenvector | last post by:
My question is more generic, but it involves what I consider ANSI standard C and portability. I happen to be a system admin for multiple platforms and as such a lot of the applications that my...
5
by: Joe | last post by:
Hi, I am looking to start a brand new project. Yeahhh! ;-) The code will do a lot of processor intensive processing. I mean a lot of loops to archieve big calculations. You know recursive...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.