473,623 Members | 3,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

It's Better CLEAR or NEW ?


I would like to hear your *opinion and advice* on best programming
practice under .NET.

Given that several time we cannot change:

MyCollection.Cl ear into the instantiation of a NEW MyCollection

because we make orphan some needed reference,

I notice also that several times I can "equivalent ly" decide whether a
collection
(or any other class) be cleared and reused or a NEW one should be
created.

This usually happens with collections having a class scope.

In such cases, where it is functionally "equivalent ", and I can
choose, what is more advisable ?

It's better to clear a collection and reuse it. Or just instantiate a
brand new one leaving the old
one to the GC ?

I mean to the purpose of memory usage, speed, GC, etc.

-P

Sep 3 '07 #1
33 1635
In article <11************ **********@r29g 2000hsg.googleg roups.com>,
pa***********@l ibero.it says...
>
I would like to hear your *opinion and advice* on best programming
practice under .NET.

Given that several time we cannot change:

MyCollection.Cl ear into the instantiation of a NEW MyCollection

because we make orphan some needed reference,

I notice also that several times I can "equivalent ly" decide whether a
collection
(or any other class) be cleared and reused or a NEW one should be
created.

This usually happens with collections having a class scope.

In such cases, where it is functionally "equivalent ", and I can
choose, what is more advisable ?

It's better to clear a collection and reuse it. Or just instantiate a
brand new one leaving the old
one to the GC ?

I mean to the purpose of memory usage, speed, GC, etc.
Just create a new one (assuming the elements of your collection don't
hold on to any unmanaged resources).

As you develop your app, keep testing it for your desired performance
benchmarks (speed, memory usage, etc...) Wait until you start to
actually see a problem to fix it.

--
Patrick Steele (pa*****@mvps.o rg)
http://weblogs.asp.net/psteele
Sep 3 '07 #2
Patrick Steele wrote:
As you develop your app, keep testing it for your desired performance
benchmarks (speed, memory usage, etc...) Wait until you start to
actually see a problem to fix it.
I couldn't disgree more, but that's just me. My goal is to build in
performance and quality, not test it in. Then again, I write primarily for
the Pocket PC and Smartphone, so I really have to think about performance.
However, 'testing-in' performance and/or quality IMHO is not the right way.

Pamela, why don't you run some tests (just a few lines) and report back to
the group which was faster and by how much. Also, if this clear/new is in
an inner loop, it seems logical that clear would be better, but ignoring the
memory allocation and garbage collection piece of the puzzle, the speed diff
would be good info.

Thanks,

Hilton
Sep 3 '07 #3
Hilton <no****@nospam. comwrote:
Patrick Steele wrote:
As you develop your app, keep testing it for your desired performance
benchmarks (speed, memory usage, etc...) Wait until you start to
actually see a problem to fix it.

I couldn't disgree more, but that's just me. My goal is to build in
performance and quality, not test it in. Then again, I write primarily for
the Pocket PC and Smartphone, so I really have to think about performance.
However, 'testing-in' performance and/or quality IMHO is not the right way.
I'm with Patrick. There's little point in micro-optimising *all* the
code (and often making it less readable at the same point) when
bottlenecks are often in unexpected places.

See

http://en.wikipedia.org/wiki/Optimiz...cience)#Quotes

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 3 '07 #4
On 3 Set, 19:13, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
Hilton <nos...@nospam. comwrote:
Patrick Steele wrote:
As you develop your app, keep testing it for your desired performance
benchmarks (speed, memory usage, etc...) Wait until you start to
actually see a problem to fix it.
I couldn't disgree more, but that's just me. My goal is to build in
performance and quality, not test it in. Then again, I write primarily for
the Pocket PC and Smartphone, so I really have to think about performance.
However, 'testing-in' performance and/or quality IMHO is not the right way.

I'm with Patrick. There's little point in micro-optimising *all* the
code (and often making it less readable at the same point) when
Well we want both MACRO and micro-Optimize our code ! Do we ? :-))

Actually this is not a minor issue because this is a task that occurs
several times into
programs and if there is a raccomended way I would tend to go that
way.

In case you are implying that CLEAR would be " less readable ", I can
say that, for instance,
to be really safe, I would always recommend to anyon to use CLEAR and
not NEW because, from my
experience the second way can be *very* dangerous, generally speaking.

You *really* must know what you are doing and watch carefully when you
indulge in a NEW
for a collection with class scope, and requires experience to stay out
of troubles.
Clearly I am talking of complex projects: in simple examples or
projects usually these issues do not appear....

I wish to hear the different opinions of yours on this matter, on the
basis of your
personal experience, especially on big programs ...
thanks :-)

-P
bottlenecks are often in unexpected places.


>
See

http://en.wikipedia.org/wiki/Optimiz...cience)#Quotes

--
Jon Skeet - <sk...@pobox.co m>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Sep 3 '07 #5
On 3 Set, 19:49, pamela fluente <pamelaflue...@ libero.itwrote:
On 3 Set, 19:13, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
Hilton <nos...@nospam. comwrote:
Patrick Steele wrote:
As you develop your app, keep testing it for your desired performance
benchmarks (speed, memory usage, etc...) Wait until you start to
actually see a problem to fix it.
I couldn't disgree more, but that's just me. My goal is to build in
performance and quality, not test it in. Then again, I write primarily for
the Pocket PC and Smartphone, so I really have to think about performance.
However, 'testing-in' performance and/or quality IMHO is not the right way.
I'm with Patrick. There's little point in micro-optimising *all* the
code (and often making it less readable at the same point) when

Well we want both MACRO and micro-Optimize our code ! Do we ? :-))

Actually this is not a minor issue because this is a task that occurs
several times into
programs and if there is a raccomended way I would tend to go that
way.

In case you are implying that CLEAR would be " less readable ", I can
say that, for instance,
to be really safe, I would always recommend to anyon to use CLEAR and
not NEW because, from my
experience the second way can be *very* dangerous, generally speaking.

You *really* must know what you are doing and watch carefully when you
indulge in a NEW
for a collection with class scope, and requires experience to stay out
of troubles.
Clearly I am talking of complex projects: in simple examples or
projects usually these issues do not appear....

I wish to hear the different opinions of yours on this matter, on the
basis of your
personal experience, especially on big programs ...

thanks :-)

-P
bottlenecks are often in unexpected places.
See
http://en.wikipedia.org/wiki/Optimiz...cience)#Quotes
--
Jon Skeet - <sk...@pobox.co m>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -
Ok I have done a few quick tests.

As to speed only, It comes out that the CLEAR way is 2 times faster
(!).
hmm, that's seems almost surprising, actually.

-P
Sep 3 '07 #6
pamela fluente <pa***********@ libero.itwrote:
I couldn't disgree more, but that's just me. My goal is to build in
performance and quality, not test it in. Then again, I write primarily for
the Pocket PC and Smartphone, so I really have to think about performance.
However, 'testing-in' performance and/or quality IMHO is not the right way.
I'm with Patrick. There's little point in micro-optimising *all* the
code (and often making it less readable at the same point) when

Well we want both MACRO and micro-Optimize our code ! Do we ? :-))
It's worth optimising *some* things: making web service interfaces
bulky instead of chatty, looking at the complexity of algorithms in
big-O notation etc. That's a far cry from trying to make every bit of
code as fast as it can possibly be just *in case* it becomes a
bottleneck.
Actually this is not a minor issue because this is a task that occurs
several times into programs and if there is a raccomended way I would
tend to go that way.
How often is "several times"? There are a lot of things which may occur
"several times" during the lifecycle of an application, but still not
contribute significantly to performance.
In case you are implying that CLEAR would be " less readable ", I can
say that, for instance, to be really safe, I would always recommend
to anyon to use CLEAR and not NEW because, from my
experience the second way can be *very* dangerous, generally speaking.
The problem is that it really *is* "generally speaking". In my view it
entirely depends on the context.
You *really* must know what you are doing and watch carefully when you
indulge in a NEW for a collection with class scope, and requires experience
to stay out of troubles.
Clearly I am talking of complex projects: in simple examples or
projects usually these issues do not appear....
In either case you need to know what you're doing. Sometimes the
collections will be visible elsewhere, sometimes they won't be.
Sometimes there's a performance impact, sometimes there isn't. It's one
of those decisions I'd look at on a case-by-case basis rather than
trying to come up with a hard and fast rule.

I wish to hear the different opinions of yours on this matter, on the
basis of your personal experience, especially on big programs ...
My personal experience is that the right answer varies by context.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 3 '07 #7
Hilton wrote:
Patrick Steele wrote:
>As you develop your app, keep testing it for your desired performance
benchmarks (speed, memory usage, etc...) Wait until you start to
actually see a problem to fix it.

I couldn't disgree more, but that's just me. My goal is to build in
performance and quality, not test it in. Then again, I write primarily for
the Pocket PC and Smartphone, so I really have to think about performance.
Everyone has to think about performance, regardless of platform.
Everyone should write code that performs well.

But for most code, all this really means is to not write inefficient
_algorithms_. Don't use an algorithm that is O(N^2), or even O(N log N)
for that matter, when an O(N) algorithm will do.

Differences in implementation of the same algorithm are not likely to
produce a performance difference that the user will notice in most
cases, while other aspects of the implementation such as overall code
maintainability and obviousness of the implementation details often do,
in the form of code that actually _works_ and doesn't have unanticipated
complications.

In addition, while one can test performance of a specific section of
code, there is not even any guarantee that such tests will translate
into a real-world application. There is more to the question of
performance than just what the basic CPU instruction timing can tell
you. For example, code that performs better in a specific scenario, but
which is larger than a similar, simpler version of the same algorithm
may in fact under-perform in other scenarios, whether due to interaction
with surrounding code or differences in the exact hardware
configuration, etc.

And of course, if you manage to squeeze a 50% improvement (an unusually
large optimization result, assuming a correct algorithm has been
designed in the first place) out of code that only consumes 1% or less
of the total execution cost, you haven't achieved anything the user will
ever care about.

So, even if you manage to prove one implementation performs better than
another in a specific situation, that isn't necessarily going to
translate into better performance for the end user.

Every software project has a finite amount of man-hours that can be
applied to it. You are doing your users a disservice if you spend some
of those man-hours optimizing code that has no need of optimization,
rather than doing things like adding features or ensuring that the code
is easily maintained, especially since those hours spent optimizing may
in fact have counter-productive results.

IMHO, the question of whether to use a new instance versus clearing an
existing one should relate more to what makes the code more readable
than which performs better. And in many cases, having an instance local
to a loop and which is initialized in each iteration of the loop is much
more readable and easily-maintained. (And in other cases, it may not be
in which case one would choose an alternate method).

As Patrick and Jon have both said, once you have a complete
implementation, then it makes sense to identify and address any
potential performance problems. At that point, you will know what areas
of the code are actually affecting the user experience, and you will be
able to measure changes in the implementation in a way that takes into
account the context of those changes.

Pete
Sep 3 '07 #8
pamela fluente wrote:
[...]
In case you are implying that CLEAR would be " less readable ", I can
say that, for instance,
to be really safe, I would always recommend to anyon to use CLEAR and
not NEW because, from my
experience the second way can be *very* dangerous, generally speaking.
How so? How is creating a new instance and assigning to a variable
"very dangerous" as compared to clearing an existing instance already
referenced by the same variable?
You *really* must know what you are doing and watch carefully when you
indulge in a NEW
for a collection with class scope, and requires experience to stay out
of troubles.
What does the scope of the variable have to do with it? If you are
clearing the instance, surely that is just as dangerous as creating a
new one for the same variable.

Pete
Sep 3 '07 #9
pamela fluente wrote:
Ok I have done a few quick tests.

As to speed only, It comes out that the CLEAR way is 2 times faster
(!).
In what context? How did you measure the difference?

It is entirely possible that clearing a collection takes half the time
that creating a new instance does. But that difference is only relevant
if that's _all_ you are doing.

Both operations should be _very_ inexpensive, so if the code that uses
the collection is doing anything that is at all interesting, I would be
surprised if you found any useful difference in time cost using one
versus the other.

Pete
Sep 3 '07 #10

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

Similar topics

5
2310
by: William Schubert | last post by:
Is there a common defintion of Clear that applies to both objects and collections? If so, where is it documented. My preconceived notion is that Clear means "reset the object to its initial state" and this applies to both collections and objects. The problem is that I cannot find anything in the literature supporting this. That would be ok, but I cannot find anything at all.
354
15780
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets hooked up to interfaces. The Gtk is more than enough gui.
13
5032
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
58
4839
by: LuisC | last post by:
What is better for holding small numbers in a program? I know that char uses less memory, but, with 32 or 64 bits systems, there are advantages (such as processing time) in using int instead of char for small numbers? Luis
34
6801
by: pamela fluente | last post by:
I would like to hear your *opinion and advice* on best programming practice under .NET. Given that several time we cannot change: MyCollection.Clear into the instantiation of a NEW MyCollection because we make orphan some needed reference, I notice also that several times I can "equivalently" decide whether a
204
4957
by: Masood | last post by:
I know that this topic may inflame the "C language Taleban", but is there any prospect of some of the neat features of C++ getting incorporated in C? No I am not talking out the OO stuff. I am talking about the non-OO stuff, that seems to be handled much more elegantly in C++, as compared to C. For example new & delete, references, consts, declaring variables just before use etc. I am asking this question with a vested interest. I...
0
8221
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
8603
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
8317
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,...
0
7134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6104
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
4067
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...
0
4154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2593
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
1
1769
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.