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

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

  1. J Luis

    J Luis Guest

    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
    1. Advertisements

  2. 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
    #2
    1. Advertisements

  3. J Luis

    J Luis Guest

    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
    #3
  4. J Luis

    J Luis Guest

    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
    #4
  5. J Luis

    J Luis Guest

    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
    #5
  6. J Luis

    Ken Davis Guest

    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
    #6
  7. J Luis

    Doug Schwarz Guest

    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
    #7
  8. J Luis

    J Luis Guest

    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
    #8
  9. J Luis

    Loren Shure Guest

    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
    #9
    1. Advertisements

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.