473,763 Members | 7,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 CreateGenericCo llection(DataTa ble 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.C reateInstance(t ); //get the type of the object I
am creating collection of
ConstructorInfo ci = t.GetConstructo r(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 CreateLabourRat eCollection(Dat aTable 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 1372
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******** ******@TK2MSFTN GP10.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 CreateGenericCo llection(DataTa ble 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.C reateInstance(t ); //get the type of the object I
am creating collection of
ConstructorInfo ci = t.GetConstructo r(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 CreateLabourRat eCollection(Dat aTable 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*******@hotm ail.nospam.com> wrote in message
news:QO******** ************@co mcast.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******** ******@TK2MSFTN GP10.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 CreateGenericCo llection(DataTa ble 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.C reateInstance(t ); //get the type of the object

I am creating collection of
ConstructorInfo ci = t.GetConstructo r(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 CreateLabourRat eCollection(Dat aTable 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*******@hotm ail.nospam.com> wrote in message
news:QO******** ************@co mcast.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******** ******@TK2MSFTN GP10.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 CreateGenericCo llection(DataTa ble 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.C reateInstance(t ); //get the type of the object
I
am creating collection of
ConstructorInfo ci = t.GetConstructo r(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 CreateLabourRat eCollection(Dat aTable 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
5284
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
7
2338
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 in an array... Thus, if I have a programming routine that utilises $a several times, it is better to write the value contained in $a to something else, for example, $vartmp, and have my routine instead use this for its work... I believe
65
12606
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 method was a piece of C code which turned out to be incorrect and incomplete but by modifieing it would still be usuable. The first method was this piece of text:
4
5649
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 able to overflow one of the memory buffers in my app, he was then able to execute code of his choosing within the security context of the service. This led to all sorts of precautionary measures such as ensuring that the service ran in a low-access...
15
9827
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 that you use in combination with the ALT key to enter special characters into a document (e.g. ALT+0147 and ALT+0148 get you 'smart quotes'). I'm trying to figure out how (or if it's possible) to obtain the Unicode equivalent of the character...
88
8078
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
14978
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 expensive is it to instantiate/use Generic Dictionaries in great numbers (let's just say 100000's ), in terms of memoryuse and performance? Any practical experiences out there?
239
10280
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 users request are a part of the OpenSource community. Many if not most of those applications strongly require the presence of the GNU compiling suite to work properly. My assumption is that this is due to the author/s creating the applications...
5
1317
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 functions and everything. Since I am a .NET programmer I have a question. Is there a real big difference in term of performance (based on the
30
3544
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
0
9564
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10148
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.