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 8201547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:51:06+00:00 2026-06-07T06:51:06+00:00

I have the following code: for i=1:N, some_mex_file(); end My MEX file does the

  • 0

I have the following code:

for i=1:N,
    some_mex_file();
end

My MEX file does the following:

  1. Declares an object, of a class I defined, that has 2 large memory blocks, i.e., 32x2048x2 of type double.
  2. Processes the data in this object.
  3. Destroys the object.

I am wondering if it takes more time when I call a MEX file in a loop that allocates large memory blocks for its object. I was thinking of migrating to C++ so that I can declare the object only once and just reset its memory space so that it can be used again and again without new declaration. Is this going to make a difference or going to be a worthless effort? In other words, does it take more time to allocate a memory in MEX file than to declare it once and reuse it?

  • 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-07T06:51:08+00:00Added an answer on June 7, 2026 at 6:51 am

    So, the usual advice here applies: Profile your code (both in Matlab and using a C/C++ profiler), or at least stop it in a debugger several times to see where it’s spending its time. Stop “wondering” about where it’s spending its time, and actually measure where it’s spending its time.

    However, I have run into problems like this, where allocating/deallocating memory in the MEX function is the major performance sink. You should verify this, however, by profiling (or stopping the code in a debugger).

    The easiest solution to this kind of performance problem is twofold:

    1. Move the loop into the MEX function. Call the MEX function with an iteration count, and let your fast C/C++ code actually perform the loop. This eliminates the cost of calling from Matlab into your MEX function (which can be substantial for large N), and facilitates the second optimization:

    2. Have your MEX function cache its allocation/deallocation, which is much, much easier (and safer) to do if you move the loop into the MEX function. This can be done several ways, but the easiest is to just allocate the space once (outside the loop), and deallocate it once the loop is done.

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

Sidebar

Related Questions

i have following code in applet that basically writes some data to a file
I have the following code to determine if a file has a string on
I have the following code in specman that I inherited: some_method() is { var
I have the following code in a WCF Service that returns a string, and
I want to get object in a list. I have following code List<Product> productList
I have the following code that grabs all controllers, sorts it, and outputs in
I have the following question: I got some code that gets called when an
So I have the following code. It's supposed to send HTML emails that were
Suppose I have the following code: class some_class{}; some_class some_function() { return some_class(); }
I currently have the following code to parse a csv file using the standard

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.