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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:50:42+00:00 2026-06-13T14:50:42+00:00

This function here is eating a lot of time in my run. But what

  • 0

This function here is eating a lot of time in my run. But what is see is the most of the time goes in the inbuilt function polyarea. Can this code be vectorized for performance boost?

Profiler Report –

  time   calls
                  1 function [S S_area] = Polygons_intersection_Compute_area(S)
                  2 % Guillaume JACQUENOT
                  3 % guillaume at jacquenot at gmail dot com
                  4 % 2007_10_08
                  5 % 2009_06_16
                  6 % Compute area of each polygon of in S.
                  7 % Results are stored as a field in S
                  8 
  0.50   51945    9 S_area = struct('A', {}); 
  0.20   51945   10 for i=1:numel(S) 
  0.28  103890   11     S(i).area = 0; 
  1.34  103890   12     S_area(i).A = zeros(1,numel(S(i).P)); 
  0.69  103890   13     for j=1:numel(S(i).P) 
  9.24  103890   14         S_area(i).A(j) = polyarea(S(i).P(j).x,S(i).P(j).y); 
  0.28  103890   15         S(i).area      = S(i).area + (1-2*S(i).P(j).hole) * S_area(i).A(j);         
  0.01  103890   16     end 
  0.08  103890   17 end 
  • 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-13T14:50:43+00:00Added an answer on June 13, 2026 at 2:50 pm

    I see 4 issues. I’ll discuss them in increasing order of potential performance gain.

    First: you use i and j as loop variable names. These are also the names of the imaginary unit in Matlab, which means Matlab will have to spend some time looking up which one you mean. Thing is, it has to do that on each iteration if the loop is not JIT’ed (which yours isn’t, I’ll get to that).

    Second: indexing multi-dimensional structures takes more time than you think. Multi-D structures are somewhat notorious in this respect, and you had better avoid too many indexing operations on them. Often making a simple copy of an element, doing all your operations on that copy, and then writing the copy back to the structure can increase performance quite a bit.

    Third: you don’t pre-allocate S_area in the most efficient way. You don’t even pre-allocate the structure, but grow it in the first loop when you pre-allocate S_area(i).A. This can all be improved (see below).

    Fourth: polyarea is not a built-in function, and so this double-loop will not be JIT’ed. If you call any function inside a loop that either you or the Mathworks wrote in M-language (rather than C), the JIT compiler will be unable to compile your loop. This is by far the most annoying (and improvable) limitation in the JIT framework, while JIT’ed loops can often run a factor of 100 or more faster than non-JIT’ed loops.

    The only solution often is to “inline” a non-builtin function in the loop body. In Matlab that means: copy-paste the entire contents of the function body into the loop, and do this recursively for all non-builtin functions called in that body.

    All of the above leads to this version of your code:

    % pre-allocate S_area
    S_area(numel(S)).A = [];
    As = cellfun(@(x) zeros(numel(x),1), {S.P}, 'UniformOutput', false);
    [S_area.A] = deal(As{:});
    
    % number of polygons for all S(ii)
    numPolys = cellfun(@numel, {S.P});
    
    % enter loop
    for ii = 1:numel(S)
        % extract S(ii) only once
        Sii = S(ii);
    
        Sii.area = 0;
        Aii = S_area(ii).A;        
        for jj = 1:numPolys(ii)
    
            p = Sii.P(jj);  % extract polygon only once
            x = p.x; % and its x and y components
            y = p.y;            
            sz = size(p);
    
            % NOTE: core of polyarea. Note that all checks and flexibility, and 
            % therefore user-friendliness, is GONE. Very little has to go wrong 
            % here before a hard-to-understand error is issued. 
            Area = reshape(abs(sum( (x([2:sz(1) 1],:) - x(:,:)).* ...
                (y([2:sz(1) 1],:) + y(:,:)))/2),[1 sz(2:end)]);
    
            Aii(jj) = Area;
            Sii.area = Sii.area + Area*(1-2*p.hole);
        end
    
        % place copies back into the strucure
        S_area(ii).A = Aii;
        S(ii).area = Sii.area;
    
    end
    

    I could not test this as properly as you can, so if you find some errors, please let me know and I’ll try to correct them.

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

Sidebar

Related Questions

I am still developing this function, but here is what I am intending it
Here in this code: $('#doneItem').click(function(){ $(this).each(function(index){ var item = 'personal' + index; alert(item); localStorage.removeItem('personal'
here's my code : $('#retouche_a_faire').click(function() { $(#retouche_a_faire_alert).toggle(this.checked); }); It's working great. Basicly, it's a
I have this simple function: <script type=text/javascript> //<![CDATA[ jQuery(function($){ function here(b){alert(b);} ; here(6); });
My javascript function looks like this - function updateSuccess(aEle) { //Here aEle doesn't have
It is valid JavaScript to write something like this: function example(x) { Here is
This function creates & stores a cookie, and here it stores the name of
Here is the situation. I have some javascript that looks like this: function onSubmit()
Maddening problem here. When my page loads: <body onload=getClientDateTime();> It runs this function: document.getElementById('ClientDateTime').value=hello
Here's the story... I have a jQuery function that does something, this function is

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.