Thread Subject: Slow Loading of Classes

Subject: Slow Loading of Classes

From: Matthew Whitaker

Date: 10 Nov, 2009 22:25:03

Message: 1 of 4

I have some classes that are handle subclasses (although several layers deep) that are very slow to load the first time they are used (on the order of 15-17 seconds to load and perform their initial tasks). All subsequent calls to create instances of the classes perform adequately fast (~2 seconds -reasonable for the tasks they do).

They do have a couple of Static methods but removing those does not seem to help any. I initially thought that this might be the culprit because when opening these functions in the editor sometimes they are very slow to open the first time.

The profiler is not very helpful. The initial call to create an instance has about the same relative profile as the subsequent ones but everything takes 10-15 times as long.

All the classes are subclassed as:
handle
^
parent1
^
parent2
^ ^ ^
class1 class2 ...

Note parents1 and 2 both have other subclasses off of them.
                                        
These are pretty complex classes but I got some that are as complex but don't exhibit this behavior.

Does anybody out there have any ideas as to what clues I should be looking for as I dissect these classes to figure out the culprit.

OR

Is there a way to quickly preload the classes to get past the initial startup.

If I can display the behavior in a stripped down code snippet I will post.

Thanks for any insights you all can give.

Subject: Slow Loading of Classes

From: Matt

Date: 10 Nov, 2009 23:44:01

Message: 2 of 4

"Matthew Whitaker" <mattlwhitaker@REMOVEgmail.com> wrote in message <hdcp7v$jh7$1@fred.mathworks.com>...

Without knowing the internals of the class, it's hard to say much.

The only thing I can think of is that the object is pointing to a very large amount of data at the time it gets saved and needs to save all that data with it in the .mat file so that it is sure to have something to point to when it gets loaded back in. Accordingly, it needs a long time to load that data back in.
(Obviously the object can't simply unshare the data when it is saved - it can have no memory of the other objects it was sharing with once it leaves the MATLAB workspace).

If there is a smarter way to initialize the object upon loading, you could write a
saveobj() method and loadobj() methods to do so. It might also help to play with the ConstructOnLoad class attribute.

Subject: Slow Loading of Classes

From: Matthew Whitaker

Date: 11 Nov, 2009 01:00:17

Message: 3 of 4

"Matt " <xys@whatever.com> wrote in message <hdcts1$57k$1@fred.mathworks.com>...
> "Matthew Whitaker" <mattlwhitaker@REMOVEgmail.com> wrote in message <hdcp7v$jh7$1@fred.mathworks.com>...
>
> Without knowing the internals of the class, it's hard to say much.
>
> The only thing I can think of is that the object is pointing to a very large amount of data at the time it gets saved and needs to save all that data with it in the .mat file so that it is sure to have something to point to when it gets loaded back in. Accordingly, it needs a long time to load that data back in.
> (Obviously the object can't simply unshare the data when it is saved - it can have no memory of the other objects it was sharing with once it leaves the MATLAB workspace).
>
> If there is a smarter way to initialize the object upon loading, you could write a
> saveobj() method and loadobj() methods to do so. It might also help to play with the ConstructOnLoad class attribute.
Matt
Thanks for the suggestions. There is a moderate amount of data being passed to the constructor ( a structure containing a 512x512 uint16 image and some image paramters) The ConstructOnLoad did not seem to help. What seems to be taking all the time in the construct is a call to a private method that does some analysis and calls an external regular function. In particular there are a couple of nested functions in that external function that seem to be taking up all the time. This is true on the first and subsequent runs although it is much worse on the initial run. The call to the external function may be the issue here as that is different to a lot of the objects I have.
Matt W

Subject: Slow Loading of Classes

From: Matthew Whitaker

Date: 11 Nov, 2009 01:23:03

Message: 4 of 4

"Matthew Whitaker" <mattlwhitaker@REMOVEgmail.com> wrote in message <hdd2b1$3rr$1@fred.mathworks.com>...
> "Matt " <xys@whatever.com> wrote in message <hdcts1$57k$1@fred.mathworks.com>...
> > "Matthew Whitaker" <mattlwhitaker@REMOVEgmail.com> wrote in message <hdcp7v$jh7$1@fred.mathworks.com>...
> >
> > Without knowing the internals of the class, it's hard to say much.
> >
> > The only thing I can think of is that the object is pointing to a very large amount of data at the time it gets saved and needs to save all that data with it in the .mat file so that it is sure to have something to point to when it gets loaded back in. Accordingly, it needs a long time to load that data back in.
> > (Obviously the object can't simply unshare the data when it is saved - it can have no memory of the other objects it was sharing with once it leaves the MATLAB workspace).
> >
> > If there is a smarter way to initialize the object upon loading, you could write a
> > saveobj() method and loadobj() methods to do so. It might also help to play with the ConstructOnLoad class attribute.
> Matt
> Thanks for the suggestions. There is a moderate amount of data being passed to the constructor ( a structure containing a 512x512 uint16 image and some image paramters) The ConstructOnLoad did not seem to help. What seems to be taking all the time in the construct is a call to a private method that does some analysis and calls an external regular function. In particular there are a couple of nested functions in that external function that seem to be taking up all the time. This is true on the first and subsequent runs although it is much worse on the initial run. The call to the external function may be the issue here as that is different to a lot of the objects I have.
> Matt W

Interestingly enough if I comment out the call to the analysis method in the constructor it still takes ~12s on the initial instantiation. So basically then it is taking all the time to go to its superclass and so forth. The profiler is useless in this case it shows no time elapsed in any of the calls to the superclasses.
Oh well , this gives me some hope that I can pare away at the code to narrow this down.
Matt W

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
oop Matt J 10 Nov, 2009 18:15:19
classdef slow c... Matthew Whitaker 10 Nov, 2009 17:29:03
rssFeed for this Thread

Contact us at files@mathworks.com