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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:38:02+00:00 2026-06-18T08:38:02+00:00

Is it possible to get 3-6x speedup from the following simple class? I am

  • 0

Is it possible to get 3-6x speedup from the following simple class?

I am trying to make a class that pretends to be an inline function but the parenthesis/subsref operator overloading doesn’t go fast enough for me.

I created the class CTestOp to replace the inline function f = @(x) A*x by letting subsref take a vector and multiplying it against the class property A.

Benchmarks indicate that for small size A and x (say, m=5) it takes 4-7x as long to use the inline function as to just write A*x and it takes 4-7x as long to use the class as to use the inline function:

Elapsed time is 0.327328 seconds for the class
Elapsed time is 0.053322 seconds for the inline function.
Elapsed time is 0.011704 seconds for just writing A*x.

I have made a series of improvements to get here but there are problems. I can see substantial gains, for instance, by not asking for this.A but then that defeats the whole purpose. I would have liked to use an abstract class that allows us to write various operation functions—but while making the class abstract didn’t add much time at all, making the actual function call did.

Any ideas?

The class is:

classdef CTestOp < handle

    properties     
        A = [];
    end

    methods
        function this = CTestOp(A)
            this.A = A;
        end

        function result = operation(this, x)
            result = this.A*x;
        end

        function result = subsref(this, S)

%             switch S.type
%                 case '()'
                    %   result = this.operation(S.subs{1});  % Killed because this was really slow
                    %   result = operation(this, S.subs{1}); % I wanted this, but it was too slow
                    result = this.A*S.subs{1};
%                 otherwise
%                     result = builtin('subsref', this, S);
%             end

        end
    end

end

While the test code is:

m = 5;
A = randn(m,m);
x = randn(m,1);

f = @(x) A*x;

myOp = CTestOp(A);

nc = 10000;

% Try with the class:
tic
for ind  = 1:nc
r_abs = myOp(x);
end
toc


% Try with the inline function:
tic
for ind = 1:nc
r_fp = f(x);
end
toc

% Try just inline. so fast!
tic
for ind = 1:nc
r_inline = A*x;
end
toc
  • 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-18T08:38:03+00:00Added an answer on June 18, 2026 at 8:38 am

    If you want to write fast code in Matlab, the trick was always to vectorize the code.
    The same holds for using Matlab OO. Though I am unable to test it at the moment I am quite confident that you can reduce the overhead by performing one big operation rather than many small ones.

    In your specific example, you can run the benchmark again and see if my statement actually holds by changing these two lines:

    m = 500; % Work with one big matrix rather than many tiny ones
    
    nc = 99; % Just some number that should give you reasonable run times
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is possible get reference to static function and run? like this: namespace vendor\foo; class
I am trying to work out if it is possible get JPA to persist
Possible Duplicate: Get file name from URI string in C# How to extract file
Possible Duplicate: Get all files from VSS for a given date? I need to
Possible Duplicate: Get URL of ASP.Net Page in code-behind I'm trying to hold current
Not sure if this is possible, but if it is it would make my
Right now, the XML response that I get from the API contains just the
Is It possible get ticks number with same format like DateTime.Ticks .NET object, that
does anyone know how to (if possible) get the owner of the tag on
Possible Duplicate: Get variable name. javascript “reflection” Is there a way to know the

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.