Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8699909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:03:44+00:00 2026-06-13T02:03:44+00:00

I have some Matlab code which needs to be speeded up. Through profiling, I’ve

  • 0

I have some Matlab code which needs to be speeded up. Through profiling, I’ve identified a particular function as the culprit in slowing down the execution. This function is called hundreds of thousands of times within a loop.

My first thought was to convert the function to mex (using Matlab Coder) to speed it up. However, common programming sense tells me the interface between Matlab and the mex code would lead to some overhead, which means calling this mex function thousands of times might not be a good idea. Is this correct? Or does Matlab do some magic when it’s the same mex being called repeatedly to remove the overhead?

If there is significant overhead, I’m thinking of restructuring the code so as to add the loop to the function itself and then creating a mex of that. Before doing that, I would like to validate my assumption to justify the time spent on this.

Update:

I tried @angainor’s suggestion, and created donothing.m with the following code:

function nothing = donothing(dummy) %#codegen
nothing = dummy;
end

Then, I created a mex function from this as donothing_mex, and tried the following code:

tic;
for i=1:1000000
    donothing_mex(5);
end
toc;

The result was that a million calls to the function took about 9 seconds. This is not a significant overhead for our purposes, so for now I think I will convert the called function alone to mex. However, calling a function from a loop that executes about a million times does seem a pretty stupid idea in retrospect, considering this is performance critical code, so moving the loop to the mex function is still in the books, but with much lesser priority.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:03:45+00:00Added an answer on June 13, 2026 at 2:03 am

    As usual, it all depends on the amount of work you do in the MEX file.. The overhead of calling MEX function is constant and does not depend on e.g., the problem size. It means that arguments are not copied to new, temporary arrays. Hence, if it is enough work, the MATLAB overhead of calling the MEX file will not show. Anyway, in my experience the MEX call overhead is significant only for the first time the mex function is called – the dynamic library has to be loaded, symbols resolved etc. Subsequent MEX calls have very little overhead and are very efficient.

    Almost everything in MATLAB is connected with some overhead due to the nature of this high-level language. Unless you have a code, which you are sure is fully compiled with JIT (but then you do not need a mex file :)) So you have a choice of one overhead over the other..

    So sum up – I would not be too scared of MEX calling overhead.

    Edit As often heard here and elsewhere, the only reasonable thing to do in any particular case is of course BENCHMARK and check it for your self. You can easily estimate the MEX call overhead by writing a trivial MEX function:

    #include "mex.h"
    void mexFunction(int nlhs, mxArray *plhs[ ], int nrhs, const mxArray *prhs[ ]) 
    {      
    }
    

    On my computer you get

    tic; for i=1:1000000; mexFun; end; toc
    Elapsed time is 2.104849 seconds.
    

    That is 2e-6s overhead per MEX call. Add your code, time it and see, if the overhead is at acceptable level, or not.

    As Andrew Janke noted below (thanks!), the MEX function overhead apparently depends on the number of arguments you pass to the MEX function. It is a small dependence, but it is there:

    a = ones(1000,1);
    tic; for i=1:1000000; mexFun(a); end; toc
    Elapsed time is 2.41 seconds.
    

    It is not related to size of a:

    a = ones(1000000,1);
    tic; for i=1:1000000; mexFun(a); end; toc
    Elapsed time is 2.41805 seconds.
    

    But it is related to the number of arguments

    a = ones(1000000,1);
    b = ones(1000000,1);
    tic; for i=1:1000000; mexFun(a, b); end; toc
    Elapsed time is 2.690237 seconds.
    

    So you might want to take that into account in your tests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some piece of code for my program in Matlab 7.10.0 which
I have written a code in matlab which modulate and demodulate some signals. I
I have been given some matlab code compiled using the .net compiler. I can
I have a problem in my MATLAB code. Let me first give you some
I have a bit of Matlab code and I am trying to export some
I have some code that plots triangles in MATLAB. I need to be able
I have a problem understanding some code fragment from Matlab Signal processing Toolbox fir2()
I have written some matlab code for image analysis that searches for clusters in
So I have a piece of MATLAB code which I am compiling in exe.
I have some data generated in MATLAB that I want to process using Perl.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.