# Which is more efficient min(min(A)) or min(A(:)) ?

Discussion in 'MATLAB' started by J Luis, Feb 12, 2006.

1. ### J LuisGuest

Hi,
If A is a 2D array which of these forms is more efficient?

In terms of computing speed there is no difference but I wonder what
happens with the memory use.

I suspect that the min(A )) form will create an internal copy of A
(as a column vector) thus using the double of the
memory, so the min(min(A)) version will be more efficient.
But this is only a suspiction.

Anyone knows more about this?

J. Luis

J Luis, Feb 12, 2006
1. ### Advertisements

2. ### John D'ErricoGuest

Why not try it yourself on your own system?

A = rand(1000);for i = 1:3,tic,B=min(min(A));toc,tic,B=min(A ));toc,end
Elapsed time is 0.094790 seconds.
Elapsed time is 0.093931 seconds.
Elapsed time is 0.091209 seconds.
Elapsed time is 0.092415 seconds.
Elapsed time is 0.091157 seconds.
Elapsed time is 0.092810 seconds.

A = rand(2000);for i = 1:3,tic,B=min(min(A));toc,tic,B=min(A ));toc,end
Elapsed time is 0.373556 seconds.
Elapsed time is 0.370009 seconds.
Elapsed time is 0.363434 seconds.
Elapsed time is 0.367133 seconds.
Elapsed time is 0.364683 seconds.
Elapsed time is 0.368840 seconds.

A = rand(3000);for i = 1:3,tic,B=min(min(A));toc,tic,B=min(A ));toc,end
Elapsed time is 0.823979 seconds.
Elapsed time is 0.828983 seconds.
Elapsed time is 0.816033 seconds.
Elapsed time is 0.827481 seconds.
Elapsed time is 0.817385 seconds.
Elapsed time is 0.829265 seconds.

HTH,
John D'Errico

--
The best material model of a cat is another, or preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945

Those who can't laugh at themselves leave the job to others.
Anonymous

John D'Errico, Feb 12, 2006
1. ### Advertisements

3. ### J LuisGuest

of
Why not try it yourself on your own system?

A = rand(1000);for i =
1:3,tic,B=min(min(A));toc,tic,B=min(A ));toc,end[/QUOTE]

John,
My question is not about the running time, which as we can
see, are virtualy the same.
What I'm interested is the memory use of each of the two options.

J. Luis

J Luis, Feb 12, 2006
4. ### J LuisGuest

To answer to my own question. On a Centrino 2 GHz 1.5 Gb RAM, ML R13

A = rand(10000);tic,B=min(min(A));toc
elapsed_time = 3.5930

A = rand(10000);tic,B=min(min(A )));toc
Out of memory...

J. Luis

J Luis, Feb 12, 2006
5. ### J LuisGuest

For the record. With a C mex file

A = rand(10000);
tic,[z1,z2]=scaleto8(A);toc
elapsed_time = 1.7190

J. Luis

J Luis, Feb 12, 2006
6. ### Ken DavisGuest

If the first one runs, I don't see why the second one shouldn't. I suspect
your problem is the the extra call to min in the second one (which probably
makes an extra copy of the array)

Ken Davis, Feb 12, 2006
7. ### Doug SchwarzGuest

The min(A )) version will create a temporary MATLAB array for the
vector version of the data, but the actual data will share memory with A
so the only memory allocation will be for whatever headers are required
for the temporary array. If A is NxM then N*M-1 actual comparisons will
have to be made.

The min(min(A)) version will create a temporary array for the result of
the first min operation. This will require memory allocation for M
elements. The total number of comparisons should be (N-1)*(M-1) = N*M -
N - M - 1 which is (N + M) fewer than above.

So I would guess that the two versions will be about equally fast but
the second version will require a little more memery.

Doug Schwarz, Feb 12, 2006
8. ### J LuisGuest

Ken,
You are right that the second works as well. The out of
memory eror is due to second call to rand, wich tries to
generate a second A, and not because min(min(A )))
internally duplicates A.
With a clean workspace I get:

A = rand(10000);tic,B=min(A ));toc
elapsed_time = 3.5780

So this proves that there is no aditional memory overload
with the min(A )) form.

J. Luis

J Luis, Feb 12, 2006
9. ### Loren ShureGuest

They should be entirely comparable for regular MATLAB arrays. For
those, when you type A ), MATLAB allocates an extra header (~80 bytes,
I think) to store the new shape, but points to the same data since the
data in the 2 arrays are equivalent. Gut, not measurement, would
suggest then that min(A )) is faster because I only need to sweep
through the data once and make no additional temporary arrays, only one
for the new output. In reality, both calls are about the same in speed
for sizes I tested (up to 2000x2000).

Loren Shure, Feb 13, 2006

Ask a Question

## Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8914215,"math_prob":0.92707074,"size":263,"snap":"2021-04-2021-17","text_gpt3_token_len":75,"char_repetition_ratio":0.10810811,"word_repetition_ratio":0.27906978,"special_character_ratio":0.31939164,"punctuation_ratio":0.20338982,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97022146,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-15T02:41:53Z\",\"WARC-Record-ID\":\"<urn:uuid:08da1517-b6e1-4f81-b445-7c2dce485d0b>\",\"Content-Length\":\"57222\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0d3a4a6b-339a-4712-baf5-e9ac58f00ffa>\",\"WARC-Concurrent-To\":\"<urn:uuid:d68530ba-70c5-488d-8da6-4294a61dda20>\",\"WARC-IP-Address\":\"104.21.58.83\",\"WARC-Target-URI\":\"https://www.math-forums.com/threads/which-is-more-efficient-min-min-a-or-min-a.129072/\",\"WARC-Payload-Digest\":\"sha1:Z6MG4AQOFBDYORZA22R2CRGEAQ5FMIP7\",\"WARC-Block-Digest\":\"sha1:RWGX7UWDF3SLBSLEGMSY6WV5AHW7LF3Z\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038082988.39_warc_CC-MAIN-20210415005811-20210415035811-00527.warc.gz\"}"}