Thread Subject:
persistent variables

Subject: persistent variables

From: Bruno Luong

Date: 22 Aug, 2012 14:54:07

Message: 1 of 4

In a recent post, I use a persistent variables (http://www.mathworks.com/matlabcentral/newsreader/view_thread/322647#885892 , side note: in order to avoid filling a calling stack in intensive recursive call).

I made a profiling of the code to see what takes time, and the PERSISTENT instructions seem to take a large amount of time.

In the past, I have observed similar poor performance issue with PERSISTENT in other contexts.

I have no idea what behind the scene. Coming from another standard programming languages, a global/static variables usually come as free meal. So MATLAB behavior surprises me.

I'm not sure someone here can (or allowed) to answer, but I try still to ask:
- what is the reason for that?
- what is a general recommendation for when or when not to use persistent variables?

Bruno

Subject: persistent variables

From: someone

Date: 22 Aug, 2012 16:17:07

Message: 2 of 4

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k12rqf$rma$1@newscl01ah.mathworks.com>...
> In a recent post, I use a persistent variables (http://www.mathworks.com/matlabcentral/newsreader/view_thread/322647#885892 , side note: in order to avoid filling a calling stack in intensive recursive call).
>
> I made a profiling of the code to see what takes time, and the PERSISTENT instructions seem to take a large amount of time.
>
> In the past, I have observed similar poor performance issue with PERSISTENT in other contexts.
>
> I have no idea what behind the scene. Coming from another standard programming languages, a global/static variables usually come as free meal. So MATLAB behavior surprises me.
>
> I'm not sure someone here can (or allowed) to answer, but I try still to ask:
> - what is the reason for that?
> - what is a general recommendation for when or when not to use persistent variables?
>
> Bruno

Just curious, does it "take a large amount of time" only the first time its encountered?

I can understand why other programming languages that are "precompiled" might give you a "free meal" when executing and there would be a penalty the first time MATLAB parses the statement. I would be surprised if thereafter the PERSISTENT variable incurred a penalty. Is using persistent still better than not using it when a function is called multiple times?

PS I am not associated with MathWorks.

Subject: persistent variables

From: Bruno Luong

Date: 22 Aug, 2012 16:41:07

Message: 3 of 4

"someone" wrote in message <k130m3$i43$1@newscl01ah.mathworks.com>...

> Just curious, does it "take a large amount of time" only the first time its encountered?

Not only. I believe every call of the function.

>
> I can understand why other programming languages that are "precompiled" might give you a "free meal" when executing and there would be a penalty the first time MATLAB parses the statement.
> I would be surprised if thereafter the PERSISTENT variable incurred a penalty.

See above.

> Is using persistent still better than not using it when a function is called multiple times?

It seems so. My guess is it must call some hash table every time an persistent statement is encountered, and not only the first time, but it's just a guess. It should have some workaround with Mex programming using mexMakeMemoryPersistent() API.

Bruno

Subject: persistent variables

From: someone

Date: 22 Aug, 2012 17:51:07

Message: 4 of 4

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k13233$nde$1@newscl01ah.mathworks.com>...
> "someone" wrote in message <k130m3$i43$1@newscl01ah.mathworks.com>...
>
> > Just curious, does it "take a large amount of time" only the first time its encountered?
>
> Not only. I believe every call of the function.
>
> >
> > I can understand why other programming languages that are "precompiled" might give you a "free meal" when executing and there would be a penalty the first time MATLAB parses the statement.
> > I would be surprised if thereafter the PERSISTENT variable incurred a penalty.
>
> See above.
>
> > Is using persistent still better than not using it when a function is called multiple times?
>
> It seems so. My guess is it must call some hash table every time an persistent statement is encountered, and not only the first time, but it's just a guess. It should have some workaround with Mex programming using mexMakeMemoryPersistent() API.
>
> Bruno

Very interesting!?!

Tags for this Thread

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.

rssFeed for this Thread

Contact us