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

collection class performance seems SLOOOWWWWWW

I have a small sellection of code here. When the "button1_Click" is
called the performance is quite poor (~ 0.5 sec). Only thing is I am
not really doing anything!! In C++ the same thing takes ~ 0 seconds to
execute!!. The List<Pumpicle> is faster than ArrayList and I convert
the List to an array before access. It just seems to be something like
unboxing the doubles out of the Pumpicle class. Any ideas??

PumpicleContainer pc = null;

private void Form1_Load(object sender, EventArgs e)
{
pc = new PumpicleContainer();

for (int i = 0; i < 10000; i++)
{
pc.Add(new Pumpicle());
}

pc.ToArray();
}

public class DaublePoint
{
public double m_x;
public double m_y;
public double m_z;
};

public class Pumpicle
{
public DaublePoint m_pos;
public double m_mass;

public Pumpicle()
{
m_pos = new DaublePoint();
}
};

class PumpicleContainer
{
public List<Pumpicle> m_pList = new List<Pumpicle>();

Pumpicle[] m_arr = null;

public void Add(Pumpicle p)
{
m_pList.Add(p);
}

public void ToArray()
{
m_arr = m_pList.ToArray();
}

public Pumpicle GetParticle(int i)
{
return m_arr[i];
}
}

private void button1_Click(object sender, EventArgs e)
{
pc.ToArray();

long start = System.DateTime.Now.Ticks;

int np = 0;
for (int idx = 0; idx < 20; idx++)
{
np += 22;
for (int i = -33; i <= 33; i++)
{
for (int j = -33; j <= 33; j++)
{
for (int l = 0; l < 5; l++)
{
for (int ps = 0; ps < np; ps++)
{
Pumpicle p = pc.GetParticle(ps);

DaublePoint pt = p.m_pos;
double x = pt.m_x;
double y = pt.m_y;
double z = pt.m_z;
}
}
}
}
}

long end = System.DateTime.Now.Ticks;

MessageBox.Show("Finished = " + ((end - start) / 1e7));
}

Jun 20 '06 #1
14 1378
I notice a couple of things.

you're doing 448900 iterations through those nested for loops (and I'm
not even counting the ps < np loop - who knows what that adds on).

You're measuring time incorrectly. instead of
System.DateTime.Now.Ticks, use System.DateTime.Now in both places. Then
subtract them and arrive at a TimeSpan, then call elapsedMillis() on the
TimeSpan object. The way you measure it now, if you happen to cross a
second boundary, you could wind up with very inconsistent execution times.

DateTime then = System.DateTime.Now;
// your loop here.
DateTime now = System.DateTime.Now;
TimeSpan ts = now - then;
Console.WriteLine(ts.TotalMilliseconds);

Jeremiah

ia*******@bmtcordah.com wrote:
I have a small sellection of code here. When the "button1_Click" is
called the performance is quite poor (~ 0.5 sec). Only thing is I am
not really doing anything!! In C++ the same thing takes ~ 0 seconds to
execute!!. The List<Pumpicle> is faster than ArrayList and I convert
the List to an array before access. It just seems to be something like
unboxing the doubles out of the Pumpicle class. Any ideas??

PumpicleContainer pc = null;

private void Form1_Load(object sender, EventArgs e)
{
pc = new PumpicleContainer();

for (int i = 0; i < 10000; i++)
{
pc.Add(new Pumpicle());
}

pc.ToArray();
}

public class DaublePoint
{
public double m_x;
public double m_y;
public double m_z;
};

public class Pumpicle
{
public DaublePoint m_pos;
public double m_mass;

public Pumpicle()
{
m_pos = new DaublePoint();
}
};

class PumpicleContainer
{
public List<Pumpicle> m_pList = new List<Pumpicle>();

Pumpicle[] m_arr = null;

public void Add(Pumpicle p)
{
m_pList.Add(p);
}

public void ToArray()
{
m_arr = m_pList.ToArray();
}

public Pumpicle GetParticle(int i)
{
return m_arr[i];
}
}

private void button1_Click(object sender, EventArgs e)
{
pc.ToArray();

long start = System.DateTime.Now.Ticks;

int np = 0;
for (int idx = 0; idx < 20; idx++)
{
np += 22;
for (int i = -33; i <= 33; i++)
{
for (int j = -33; j <= 33; j++)
{
for (int l = 0; l < 5; l++)
{
for (int ps = 0; ps < np; ps++)
{
Pumpicle p = pc.GetParticle(ps);

DaublePoint pt = p.m_pos;
double x = pt.m_x;
double y = pt.m_y;
double z = pt.m_z;
}
}
}
}
}

long end = System.DateTime.Now.Ticks;

MessageBox.Show("Finished = " + ((end - start) / 1e7));
}

Jun 21 '06 #2

ia*******@bmtcordah.com wrote:
Only thing is I am not really doing anything!! In C++ the same thing takes ~ 0 seconds to execute!!. int np = 0;
for (int idx = 0; idx < 20; idx++)
{
np += 22;
for (int i = -33; i <= 33; i++)
{
for (int j = -33; j <= 33; j++)
{
for (int l = 0; l < 5; l++)
{
for (int ps = 0; ps < np; ps++)
{
Pumpicle p = pc.GetParticle(ps);

DaublePoint pt = p.m_pos;
double x = pt.m_x;
double y = pt.m_y;
double z = pt.m_z;
}
}
}
}
}


Be very, very careful about comparisons like this! You are right: the
code isn't doing anything useful. The C++ compiler may be smart enough
to detect that only the outermost loop has any lasting effect: it
changes np. If the compiler is clever enough to know that
pc.GetParticle(ps) has no side-effects, then it may simply eliminate
the inner loops altogether.

Are you sure that in C++ pc.GetParticle is being called the number of
times you expect? Or is it never called at all?

Then again it may simply be that the C++ compiler produces much better
code. However, you can't tell just from doing a benchmark like this
without further checks.

Jun 21 '06 #3
<ia*******@bmtcordah.com> a écrit dans le message de news:
11**********************@i40g2000cwc.googlegroups. com...

|I have a small sellection of code here. When the "button1_Click" is
| called the performance is quite poor (~ 0.5 sec). Only thing is I am
| not really doing anything!! In C++ the same thing takes ~ 0 seconds to
| execute!!. The List<Pumpicle> is faster than ArrayList and I convert
| the List to an array before access. It just seems to be something like
| unboxing the doubles out of the Pumpicle class. Any ideas??

1. Why convert the list to an array before access ? this takes time !!

2. You are not unboxing anything as you are holding doubles and retrieving
doubles.

Instead of your PumpicleContainer class, just use a List<Pumpicle> as it is
without any wrapper class and see if that makes any difference.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 21 '06 #4
1. Why convert the list to an array before access ? this takes time !!
It takes time but nothing compared to accessing the List directly
several times over. The array is faster to access than the list.
2. You are not unboxing anything as you are holding doubles and retrieving
doubles.

You are right I was suggesting something like that going on.
Instead of your PumpicleContainer class, just use a List<Pumpicle> as it is
without any wrapper class and see if that makes any difference.


I have made the ToArray function return the array and then the code
uses that. That is faster (thanks). Again Array is faster than List<>
which is way faster than ArrayList

Jun 22 '06 #5
> Be very, very careful about comparisons like this! You are right:
the
code isn't doing anything useful. The C++ compiler may be smart enough
to detect that only the outermost loop has any lasting effect: it
changes np. If the compiler is clever enough to know that
pc.GetParticle(ps) has no side-effects, then it may simply eliminate
the inner loops altogether.


The problem is actually I am comparing C# with FORTRAN. I have ported
the code over from FORTRAN and then proceded to tell my colleges how
much faster the C# would be. Then I ran the code go my ass smacked!!
The bottleneck was in this code loop. Remember that FORTRAN was used by
dinosuars, how can it be faster than C#!!!

Jun 22 '06 #6
> DateTime then = System.DateTime.Now;
// your loop here.
DateTime now = System.DateTime.Now;
TimeSpan ts = now - then;
Console.WriteLine(ts.TotalMilliseconds);


With the world cup on I would say "back of the net". Completly correct
:-). The timing was meant to be figurative but it pays to be precise.
Thanks.

Jun 22 '06 #7
<ia*******@bmtcordah.com> wrote:
1. Why convert the list to an array before access ? this takes time !!


It takes time but nothing compared to accessing the List directly
several times over. The array is faster to access than the list.


If you rearranged your loops to do what it currently the inner loop as
the outer loop, you'd be accessing the list much, much, much less
often, however. Of course, it's difficult to know whether or not that's
feasible when your code doesn't do anything useful with idx, i, j, or l
though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 22 '06 #8
ia*******@bmtcordah.com wrote:
Be very, very careful about comparisons like this! You are right:

the
code isn't doing anything useful. The C++ compiler may be smart enough
to detect that only the outermost loop has any lasting effect: it
changes np. If the compiler is clever enough to know that
pc.GetParticle(ps) has no side-effects, then it may simply eliminate
the inner loops altogether.


The problem is actually I am comparing C# with FORTRAN. I have ported
the code over from FORTRAN and then proceded to tell my colleges how
much faster the C# would be. Then I ran the code go my ass smacked!!
The bottleneck was in this code loop. Remember that FORTRAN was used by
dinosuars, how can it be faster than C#!!!


It wouldn't surprise me at all that FORTRAN / COBOL / C might be
_faster_ than C#. If anyone claimed to me that C# is _faster_ then I
would immediately be suspicious.

C# comes with tremendous advances in the ability to organize code (O-O
/ visual designers, etc), advances in security, interoperability with
databases, etc.... but speed? No, I don't think so.

If we're writing a sophisticated multi-cultural / multi-language
WinForms application that calls Web Services or reads from a database,
and I'm using C# and you're using C, I'm sure that I could _write_ the
code much, much faster than you could, but would my code _run_ faster
in the end? Probably after my two weeks of coding and your ten months
of coding, your code would probably run faster, but that's not the
"why" of using C# in that scenario, is it?

Jun 23 '06 #9
> If you rearranged your loops

Thanks, but that wasn't the point of the exercise. It is to demonstrate
how seemingly inefficient C# can be at times especially concerning the
collection classes.

Jun 24 '06 #10
> If anyone claimed to me that C# is _faster_ then I
would immediately be suspicious.


Many times C# IS faster. Remember that the IL is compiled (on the fly)
down to specific native code. This isn't for a generic x86 processor,
but for your processor (with all extensions). I have done many
comparisons and sometimes C# actually beats C++ for performance. If you
look at the example code, I create in 10000 objects and put them into
the container. Raise that up to 100000 and you would have no probs. Do
the same in C++ and you would want to do a SetSize() before you tried
otherwise you may be waiting some time.

Jun 24 '06 #11
<ia*******@bmtcordah.com> wrote:
If you rearranged your loops


Thanks, but that wasn't the point of the exercise. It is to demonstrate
how seemingly inefficient C# can be at times especially concerning the
collection classes.


But the point is that in the real world, such efficiency is very rarely
an issue. Yes, list access can be slower than array access. *Most* (not
all, but most) applications aren't going to have performance
bottlenecks due to this. They'll probably have bottlenecks due to
database access, IO, etc - or architectural issues which can be solved
by doing less work rather than doing the same work more efficiently.
Where there *are* problems like this, they can often be worked round
with micro-optimisation (such as rearranging loops) where it's been
proven to be a problem.

If I set up a microbenchmark to test simple object construction on the
heap, I suspect C# would beat C++ due to the way the managed heap works
- does that mean C++ object creation is "SLOOOWWWWWW"? No - it just
means that different environments have different strong points. Most of
the time, these strong points won't be a significant issue.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 24 '06 #12
I agree, and in addition, I want to highlight that the IL code is compiled
only the first time that you use, so the next use of this part of the IL
code don't need to recompile and waste this time another time

Xavier Jorge
<ia*******@bmtcordah.com> escribió en el mensaje
news:11**********************@c74g2000cwc.googlegr oups.com...
If anyone claimed to me that C# is _faster_ then I
would immediately be suspicious.


Many times C# IS faster. Remember that the IL is compiled (on the fly)
down to specific native code. This isn't for a generic x86 processor,
but for your processor (with all extensions). I have done many
comparisons and sometimes C# actually beats C++ for performance. If you
look at the example code, I create in 10000 objects and put them into
the container. Raise that up to 100000 and you would have no probs. Do
the same in C++ and you would want to do a SetSize() before you tried
otherwise you may be waiting some time.

Jun 25 '06 #13
All well and good. Nonetheless, I wouldn't _choose_ C# for its speed.
It has lots of other wonderful advantages, and it may run faster
sometimes, but when I think of reasons why I would want to use C#,
speed is not the first thing that comes to mind.

Don't forget that even if the JITter generates better code, you have to
contend with the garbage collecter, which can kick in any time it likes
and there goes a bunch of that time you may have gained with quicker
code.

If I were worried about raw speed, I probably wouldn't choose a garbage
collected language.

Jun 27 '06 #14
Bruce Wood wrote:
All well and good. Nonetheless, I wouldn't _choose_ C# for its speed.
It has lots of other wonderful advantages, and it may run faster
sometimes, but when I think of reasons why I would want to use C#,
speed is not the first thing that comes to mind.

Don't forget that even if the JITter generates better code, you have to
contend with the garbage collecter, which can kick in any time it likes
and there goes a bunch of that time you may have gained with quicker
code.

If I were worried about raw speed, I probably wouldn't choose a garbage
collected language.


Well, garbage collection incurs performance hits at unpredictable times
- but it can often actually give a "win" in terms of overall
performance compared with explicit malloc/free. The managed heap allows
for incredibly quick allocation, and if a whole block of objects can be
freed in one go, that can be quicker than lots of calls to free.

To my mind, the problem with garbage collection isn't the overall speed
but the fact that it can occasionally "stop the world" for significant
amounts of time.

Jon

Jun 27 '06 #15

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

Similar topics

24
by: Yang | last post by:
I found a very strange behavior when I write a C# windows application. If I allocate a huge chunk of memory by an array, the memory will never be released by .NET. The problem can be demostrated...
5
by: Audrius | last post by:
Hello, what is the best solution to hold the collection of Objects. Lets say I have such class: class Task{ private: CString title; CString description; CString owner;
5
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use...
2
by: Andrea Williams | last post by:
I need to expose the properties of an User Object to all other classes, but hide the operations (functions) from all but the Business layer of my app? The goal is to load up this USER object and...
5
by: NotABug | last post by:
Is it possible to do collections of struct __values in MC++ 2003? How can I translate the following C# example to C++: public sealed class PointsCollection : CollectionBase { public void...
6
by: Andy | last post by:
Along with many others I've noticed the large amount of memory that can be taken up by the aspnet_wp.exe. I've found that I can better control and limit this memory consumption by including a...
13
by: Scott Meddows | last post by:
I'm writing an NT service to provide security information enterprise wide. I would like to have all of this information cached memory if a previous request loaded it into memory. Then using a...
18
by: Larry Herbinaux | last post by:
I'm having issues with garbage collection with my long-standing service process. If you could review and point me in the right direction it would be of great help. If there are any helpful...
28
by: Michael Primeaux | last post by:
What is the recommended pattern for implementing a synchronized (thread-safe) class that inherits from Collection<T>? For example, I want to implement a SyncRoot property . I do see where I can...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.