Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Evaluating time of a real multiplication

Subject: Evaluating time of a real multiplication

From: Ruben Grigoryan

Date: 20 Dec, 2012 21:55:10

Message: 1 of 2

Hello,

I am trying to evaluate how long does it take i) for one CPU core to perform a multiplication of two real numbers, ii) to read/write one number to RAM. Numbers are doubles. As I have to find more than one unknown I am planning to run several simulations (schematically):

tic
2*2;
toc ; data 2. Just multiplication

tic
z(ii) = 2*2;
toc ; data 3. Multiplication and one writing

tic
x(ii)*y(ii);
toc ; data 4. Multiplication and two reading

tic
z(ii) = x(ii)*y(ii);
toc ; data 5. Multiplication, two reading and one writing

Surprisingly I get very strange data -- z(ii)=2*2 is executed faster than 2*2 and z(ii)=x(ii)*y(ii) faster than x(ii)*y(ii)!
  
What I do exactly:
1) Set affinity of the MATLAB process to one CPU core. I do it via Task Manager.
2) Run the following code:
clc;
clear;

W = 32;
K = 10^5;
tt = zeros(5,W)
for jj=1:W
% jj
    X = randn(K*jj,1);
    Y = randn(K*jj,1);
    Z = zeros(K*jj,1);
    fprintf('Data size =%d \n',K*jj);
    
    tStart = tic;
    for ii=1:K*jj
% 2*2;
    end;
    tStop = toc(tStart);
    tt(1,jj)=tStop;
    
    tStart = tic;
    for ii=1:K*jj
       2*2;
    end;
    tStop = toc(tStart);
    tt(2,jj)=tStop;
    
    tStart = tic;
    for ii=1:K*jj
       Z(ii)=2*2;
    end;
    tStop = toc(tStart);
    tt(3,jj)=tStop;
    
    tStart = tic;
    for ii=1:K*jj
       X(ii)*Y(ii);
    end;
    tStop = toc(tStart);
    tt(4,jj)=tStop;
    
    tStart = tic;
    for ii=1:K*jj
       Z(ii) = X(ii)*Y(ii);
    end;
    tStop = toc(tStart);
    tt(5,jj)=tStop;
end;

figure;
ColorOrd = get(gca,'ColorOrder');
for ii=1:5
    plot(1*K:1*K:K*W, tt(ii,:), 'Linewidth', 2, 'Color', ColorOrd(ii,:)), hold on;
end;
grid on;
% set(gca,'XTick', 1*K:1*K:K*W );
xlabel('Vectors'' size');
ylabel('Time');
hleg = legend('show');
set(hleg,'Location','NorthWest');

I am on Lenovo ThinkPad W510, Intel i7 vPro with 8 cores i7 Q820, 16 GB RAM, Windows 7 Pro and MATLAB R2010b.

So, my question is what is really happens between CPU and RAM when I run code 2*2, z(ii)=2*2, x(ii)*y(ii) and z(ii)=x(ii)*y(ii)?
Is there is any other ways to evaluate the multiplication and read/write time?

Thank you in advance.

Subject: Evaluating time of a real multiplication

From: James Tursa

Date: 20 Dec, 2012 23:42:10

Message: 2 of 2

"Ruben Grigoryan" <rug@es.aau.dk> wrote in message <kb01fu$fe7$1@newscl01ah.mathworks.com>...
> Hello,
>
> I am trying to evaluate how long does it take i) for one CPU core to perform a multiplication of two real numbers, ii) to read/write one number to RAM. Numbers are doubles. As I have to find more than one unknown I am planning to run several simulations (schematically):
>
> tic
> 2*2;
> toc ; data 2. Just multiplication

(snip)

This is an exercise in futility. The actual time spent doing the multiplication itself is swamped by the overhead of other things going on, such as getting a MATLAB variable to represent the number 2 (100+ bytes of variable structure overhead), setting up a function call to do the multiplication, creating a MATLAB variable to hold the result (another 100+ bytes of variable structure overhead), and clearing temporary variables. The only meaningful timings would be when the calculations within the loops themselves dominate the run-time, but you don't have that situation. I would recommend you stop wasting your time with this ...

James Tursa
 

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us