Thread Subject:
huge speed difference of "isempty" with and without output argument

Subject: huge speed difference of "isempty" with and without output argument

From: Markus Leuthold

Date: 17 Aug, 2012 09:18:07

Message: 1 of 7

a=1;
N=1000000;
tic
for k=1:N
    b=isempty(a);
end
toc
-> Elapsed time is 0.006023 seconds.

tic
for k=1:N
    isempty(a);
end
toc
-> Elapsed time is 0.554942 seconds.

Why the second version which doesn't have an output argument so much slower?

best regards
Kusi

Subject: huge speed difference of "isempty" with and without output argument

From: Bruno Luong

Date: 17 Aug, 2012 09:25:08

Message: 2 of 7

"Markus Leuthold" wrote in message <k0l28f$54r$1@newscl01ah.mathworks.com>...
>
> Why the second version which doesn't have an output argument so much slower?

 help ans

Bruno

Subject: huge speed difference of "isempty" with and without output argument

From: Barry Williams

Date: 17 Aug, 2012 10:39:07

Message: 3 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k0l2lk$6dh$1@newscl01ah.mathworks.com>...
> "Markus Leuthold" wrote in message <k0l28f$54r$1@newscl01ah.mathworks.com>...
> >
> > Why the second version which doesn't have an output argument so much slower?
>
> help ans
>
> Bruno

There is an output variable in both of the OP's examples. When no other is specified, the function defaults to the variable ans. Markus may already understand that. At first I was presuming that when the output was explicitly sent to to a, it was quicker because the variable does not to be created, but only the value needs to be replaced; whereas, without a specified variable, the default ans output variable is recreated each time.
But then I tried something like his examples:

tic; for n=1:1000000; b=isempty(a); end; toc
Elapsed time is 0.704933 seconds.
>> tic; for n=1:1000000;i sempty(a); end; toc
Elapsed time is 0.692797 seconds.

Now I'm curious about what is going on here. For the most part, the code that I write doesn't need to be rigorously optimized, other than obvious steps like pre-allocating arrays and so on. On occasion, however, I do find myself dealing with large data sets and would like a bit more explanation of the behavior above.

Barry

Subject: huge speed difference of "isempty" with and without output argument

From: Bruno Luong

Date: 17 Aug, 2012 10:58:06

Message: 4 of 7

"Barry Williams" <barry.r.williamsnospam@saic.com> wrote in message <k0l70b$j73$1@newscl01ah.mathworks.com>...

>
> tic; for n=1:1000000; b=isempty(a); end; toc
> Elapsed time is 0.704933 seconds.
> >> tic; for n=1:1000000;i sempty(a); end; toc
> Elapsed time is 0.692797 seconds.
>
> Now I'm curious about what is going on here. For the most part, the code that I write doesn't need to be rigorously optimized, other than obvious steps like pre-allocating arrays and so on. On occasion, however, I do find myself dealing with large data sets and would like a bit more explanation of the behavior above.

To be absolutely rigorous, you should actually time the code within a function interface so that the JIT-accelerator is fully operational.

Beside that I can't reproduce your result with 2012A. All I know is it is not advisable to scramble many statements in one line, that could hurt the performance (!).

Back to the question, I guess when the code is run on command window, there is some management related to ANS that stores the result within a persistent variable, and there is some overhead associated with it.

Bruno

Subject: huge speed difference of "isempty" with and without output argument

From: Justin Ashmall

Date: 17 Aug, 2012 12:41:08

Message: 5 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
>
> Beside that I can't reproduce your result with 2012A. All I know is it is not advisable to scramble many statements in one line, that could hurt the performance (!).
>

I can reproduce the same results in 2012a:

>> a = 1;
>> tic; for n=1:1000000; b=isempty(a); end; toc
Elapsed time is 0.002537 seconds.
>> tic; for n=1:1000000; isempty(a); end; toc
Elapsed time is 0.287180 seconds.
>> version
ans =
7.14.0.739 (R2012a)

That's a good spot by Markus, I have to say I'm pretty surprised!

Perhaps it's because the JIT compiler can't/won't optimise assignment to ans?

FWIW, this is even slower:
>> tic; for n=1:1000000;[~]=isempty(a); end; toc
Elapsed time is 1.684431 seconds.

Justin

Subject: huge speed difference of "isempty" with and without output argument

From: Bruno Luong

Date: 17 Aug, 2012 12:52:06

Message: 6 of 7

"Justin Ashmall" wrote in message <k0le53$eej$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> >
> > Beside that I can't reproduce your result with 2012A. All I know is it is not advisable to scramble many statements in one line, that could hurt the performance (!).
> >
>
> I can reproduce the same results in 2012a:
>
> >> a = 1;
> >> tic; for n=1:1000000; b=isempty(a); end; toc
> Elapsed time is 0.002537 seconds.
> >> tic; for n=1:1000000; isempty(a); end; toc
> Elapsed time is 0.287180 seconds.
> >> version
> ans =
> 7.14.0.739 (R2012a)

I did not refer to Marcus's result where no-assignment is significant slower. This can be explained by ANS.

I'm unable to reproduce Barry's result where both timings are comparable (~0.7 sec).

Bruno

Subject: huge speed difference of "isempty" with and without output argument

From: Markus Leuthold

Date: 19 Sep, 2012 12:53:08

Message: 7 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k0l2lk$6dh$1@newscl01ah.mathworks.com>...
> "Markus Leuthold" wrote in message <k0l28f$54r$1@newscl01ah.mathworks.com>...
> >
> > Why the second version which doesn't have an output argument so much slower?
>
> help ans
>
> Bruno

It seems to be indeed "ans" which is the bottleneck here. Just to make sure "isempty" is not involved in that slowdown:

>> tic; for n=1:1000000; b=7; end; toc
Elapsed time is 0.006242 seconds.
>> tic; for n=1:1000000; 7; end; toc
Elapsed time is 0.041584 seconds.

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