Solved

Performance drop while creating over 3000 object!

Posted on 2004-03-25
10
179 Views
Last Modified: 2010-04-15
have made a custom user control that works like a grid, each cell on the grid is represented by a another custome control, which is just a box with a text drawing in it!(using Graphics) now, the grid reads the info from a dataset and created instances of those cell objects, the problem is it takes up to 20 seconds to load. would there be anyway to increase the performance of creating alot of object?

0
Comment
Question by:saturn_one
10 Comments
 
LVL 22

Expert Comment

by:_TAD_
ID: 10681950


I would suggest creating arrays of objects

myObject[] myObj = new myObject[3000];


then multi-thread your application and only create the objects that you are viewing to start with.  Render your form.  and while the user is contemplating what to do, create other objects in bulk in several threads.


<thread1>
for(int i=0;i<1000;i++)
    myObj[i] = new myObject();


<thread2>
for(int i=1000;i<2000; i++)
  myObj[i] = new myObject();



<thread3>
for(int i=2000;i<3000; i++)
  myObj[i] = new myObject();
0
 

Author Comment

by:saturn_one
ID: 10682997
i'm already using an array, but when i try to multithread the application it gives me the, controls created on a thread can't be parented on another thread error!

I don't know if there is a way around that, but right now i'm just trying to increase the actual performance!


thanks for the reply.
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10684642
There is an old VB cheat I remember back from the VB4/5 days.  Instead of having controls for each cell of the grid, you only use 1 control, size it and place it over the currently selected cell so it accepts the user's inputs.  When the user moves off, put the results of the editing into the overlayed cell.

Now, this takes a bit of bookkeeping, but since the only you usually need a special control is on entry, this lets you shrink the memory usage (and load times) of the form by at least 3 orders of magnitude.
0
 
LVL 5

Expert Comment

by:AdrianJMartin
ID: 10685630
first step is to locate where the bottleneck occurs, i would suggest the free Profiler from

http://www.compuware.com/products/devpartner/profiler/default.asp


it takes a bit of learning but is well worth it.


I suspect the bottleneck is the generation of the 3000+ controls, the trick here would be to make a control that looks and behaves  like 3000+ controls, well not even 3000 because only a couple of hundred could be visible at once.....separate the data from the presentation.....
0
 
LVL 2

Accepted Solution

by:
eric_duncan earned 400 total points
ID: 10691280
You could also take the approach used by most Word processors and the like by implementing the Flyweight design pattern. To quote from the GoF's Design Patterns: Elements of Reusable Object-Oriented Software, the "Flyweight pattern describes how to share objects to allow their use at fine granularities without prohibitive cost." I have used this pattern in similar situations to great effect.

Basically, what you do is separate the properties that are common to all objects of a given class, and pass in the granular information as method parameters. So, if you were building a word processing application, you might create a single instance of a character object to draw every instance of the lowercase letter 'a', but in the method that actually outputs the letter to the screen you can pass in the formatting options (ie. bold, underlined, etc) that are relevant to the specific instance of the letter on the page. That way you can store the state information in a high-performance array and use this to update the properties of your object pool as necessary. Both dfiala13 and AdrianJMartin have hinted at this, and it is quite easy to manage once you get the core pattern set up.

You can look at http://www.dofactory.com/patterns/PatternFlyweight.aspx for an overview of the pattern.

Let me know if this helps!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:saturn_one
ID: 10692039
this sounds really intresting, but i couldn't find any examples on how to use the Flyweight on windows form controls, could someone confirm that its possible to have couple windows form controls with diffrent position size and back color using Flyweight which handdle events properly too!
0
 
LVL 2

Expert Comment

by:eric_duncan
ID: 10694803
Actually, it isn't possible to have the same instance of a windows form control in two places at once, but what you could do is create only the number of form controls you need to see at one time and change their contents as the user scrolls through the entire data set by capturing the OnScoll event. Or, if you really wanted to do a "pure" implementation of the Flyweight pattern, you could dispense with the Windows Forms Controls altogether, use a Panel Control on the form to provide a graphics surface, and create classes that would paint their contents on to the panel using GDI without using any forms controls. This is a little bit harder to implement and would only make sense if you didn't need all of the additional functionality that the Forms controls would provide, but the performance gains would be significant.

At any rate, you should be able to limit the number of objects to the small set that is visible at any given time.
0
 

Author Comment

by:saturn_one
ID: 10695549
well, my controls are already being drawn using the GDI but they still inherate windows.forms.control class, how can i capture the mouse events if i don't use the windows controls class???
0
 
LVL 2

Expert Comment

by:eric_duncan
ID: 10697069
If you need to capture mouse events, then I would probably stick with the windows controls classes. To maximize performance, I would modify the Flyweight pattern by creating enough instances of each control to fill the visible area of the screen, use a scrollbar to navigate through the data, and capture the OnScroll event to change the state of the controls.
0
 
LVL 2

Expert Comment

by:eric_duncan
ID: 10722732
Thanks for the points! Let me know if you have any more questions.

Eric
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now