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:
30% faster for-loop when changing loop variable from default(double) to int

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Hao Song

Date: 14 May, 2012 15:31:07

Message: 1 of 18

When testing a very simple program and trying to change the for-loop variable from double to int (Why did I do that?), I found the program becomes faster, about 30%.
The program is as following:

function k=pbug_d(N)
    p=0.3;
    
    for i=1:N
        p=3.4*(p*(1-p));
    end

k=p;
end

On my PC (Pentium 6700, Win7, MatlabR2010b), the command

tic, pbug_d(1e9),toc

took 9.3s.

When I changed the for-loop variable to int type:

 function k=pbug_i(N)
    
    p=0.3;
    
    i=int32(1);
    
    for i=1:N
        p=3.4*(p*(1-p));
    end
    k=p;
end

the command

tic, pbug_i(int32(1e9)),toc %the input type should also be int

gave the same result(ans= 0.451963247626153)
and took only 6.6s, ~30% faster!

This result sounds good. A simple data type change brings 30% better performance.
I also tested them under Linux (Ubuntu 12.04, LTS, MatlabR2010b) and got the similar result.
While when I compiled the .m file into mex file, there is little difference for int and double for-loop variable. In linux, the compiled mex using int loop variable even becomes a little slower than double type. Anyone knows why?

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: matt dash

Date: 14 May, 2012 17:16:09

Message: 2 of 18

"Hao Song" wrote in message <jor8fr$a4n$1@newscl01ah.mathworks.com>...
> When testing a very simple program and trying to change the for-loop variable from double to int (Why did I do that?), I found the program becomes faster, about 30%.
> The program is as following:
>
> function k=pbug_d(N)
> p=0.3;
>
> for i=1:N
> p=3.4*(p*(1-p));
> end
>
> k=p;
> end
>
> On my PC (Pentium 6700, Win7, MatlabR2010b), the command
>
> tic, pbug_d(1e9),toc
>
> took 9.3s.
>
> When I changed the for-loop variable to int type:
>
> function k=pbug_i(N)
>
> p=0.3;
>
> i=int32(1);
>
> for i=1:N
> p=3.4*(p*(1-p));
> end
> k=p;
> end
>
> the command
>
> tic, pbug_i(int32(1e9)),toc %the input type should also be int
>
> gave the same result(ans= 0.451963247626153)
> and took only 6.6s, ~30% faster!
>
> This result sounds good. A simple data type change brings 30% better performance.
> I also tested them under Linux (Ubuntu 12.04, LTS, MatlabR2010b) and got the similar result.
> While when I compiled the .m file into mex file, there is little difference for int and double for-loop variable. In linux, the compiled mex using int loop variable even becomes a little slower than double type. Anyone knows why?

Both versions are the same speed for me. I dont see why it would matter if you have i=int32(1) before the for loop... the for loop redefines i... it shouldn't matter what class i was before the loop.

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: James Tursa

Date: 14 May, 2012 18:15:08

Message: 3 of 18

"matt dash" wrote in message <jorekp$86u$1@newscl01ah.mathworks.com>...
>
> Both versions are the same speed for me. I dont see why it would matter if you have i=int32(1) before the for loop... the for loop redefines i... it shouldn't matter what class i was before the loop.

The only thing I can think of is it might affect how the JIT optimizes the code inside the function, although I agree with you that I don't see why it *should* affect this.

James Tursa

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Bruno Luong

Date: 14 May, 2012 18:44:10

Message: 4 of 18

I agree with Matt and James. There are many things that can lead to wrong timings and false conclusion. The risk is greatlly reduced by using this FEX tool:

http://www.mathworks.fr/matlabcentral/fileexchange/18798-timeit-benchmarking-function

Bruno

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Hao Song

Date: 15 May, 2012 02:21:07

Message: 5 of 18

Since software timing is not so precise, I use a stopwatch to manually compare the time taken by these two commands
pbug_i(int32(2e9)) vs pbug_d(2e9)
12.9s vs 18.1s

I tried it on another PC with i3-2100 cpu,
the result is
15.2s vs 18.9s

It seems that the speed-up is real and CPU dependent.

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jorjpq$266$1@newscl01ah.mathworks.com>...
> I agree with Matt and James. There are many things that can lead to wrong timings and false conclusion. The risk is greatlly reduced by using this FEX tool:
>
> http://www.mathworks.fr/matlabcentral/fileexchange/18798-timeit-benchmarking-function
>
> Bruno

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Hao Song

Date: 15 May, 2012 02:31:07

Message: 6 of 18

And what really matters is the type of N,
pbug_d(2e9) vs pbug_d(int32(2e9))
Manually stopwatch measurement gave
12.7s vs 17.8s
"Hao Song" wrote in message <joseij$t1m$1@newscl01ah.mathworks.com>...
> Since software timing is not so precise, I use a stopwatch to manually compare the time taken by these two commands
> pbug_i(int32(2e9)) vs pbug_d(2e9)
> 12.9s vs 18.1s
>
> I tried it on another PC with i3-2100 cpu,
> the result is
> 15.2s vs 18.9s
>
> It seems that the speed-up is real and CPU dependent.
>
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jorjpq$266$1@newscl01ah.mathworks.com>...
> > I agree with Matt and James. There are many things that can lead to wrong timings and false conclusion. The risk is greatlly reduced by using this FEX tool:
> >
> > http://www.mathworks.fr/matlabcentral/fileexchange/18798-timeit-benchmarking-function
> >
> > Bruno

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Roger Stafford

Date: 15 May, 2012 04:19:15

Message: 7 of 18

"Hao Song" wrote in message <josf5b$1v8$1@newscl01ah.mathworks.com>...
> And what really matters is the type of N,
> pbug_d(2e9) vs pbug_d(int32(2e9))
> Manually stopwatch measurement gave
> 12.7s vs 17.8s
- - - - - - - -
  Since the type of the argument N and therefore i is changed from double to int32 as Hao has indicated in the last two articles, the difference in speeds seems quite plausible to me. It ought to be faster to increment a counter by 1 in int32 format than a 64-bit floating point format number.

Roger Stafford

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Bruno Luong

Date: 15 May, 2012 06:44:19

Message: 8 of 18

i5, Windows7, 2012a 64 bit

>> tic; pbug(1e9); toc
Elapsed time is 11.442164 seconds.

>> tic; pbug(uint32(1e9)); toc
Elapsed time is 11.675563 seconds.
>>

With:

function k=pbug(N)
p=0.3;
for i=1:N
    p=3.4*(p*(1-p));
end
k=p;
end

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Matt J

Date: 15 May, 2012 13:27:07

Message: 9 of 18

"Roger Stafford" wrote in message <joslg3$p6u$1@newscl01ah.mathworks.com>...
>
> Since the type of the argument N and therefore i is changed from double to int32 as Hao has indicated in the last two articles, the difference in speeds seems quite plausible to me. It ought to be faster to increment a counter by 1 in int32 format than a 64-bit floating point format number.
===============


The loop counter in Hao's examples is not in int32 type, though.
The pre-casting of i to int32 is negated once the for-loop begins:

>> i=int32(1); for i=1:3, class(i), end

ans =

double


ans =

double


ans =

double

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Matt J

Date: 15 May, 2012 13:52:06

Message: 10 of 18

"Matt J" wrote in message <jotljb$42u$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <joslg3$p6u$1@newscl01ah.mathworks.com>...
> >
> > Since the type of the argument N and therefore i is changed from double to int32 as Hao has indicated in the last two articles, the difference in speeds seems quite plausible to me. It ought to be faster to increment a counter by 1 in int32 format than a 64-bit floating point format number.
> ===============
>
>
> The loop counter in Hao's examples is not in int32 type, though.
> The pre-casting of i to int32 is negated once the for-loop begins:
============

Never mind, I didn't notice that N was also pre-cast.

Still, when I run both versions, I don't see a significant speed difference either.

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Roger Stafford

Date: 15 May, 2012 15:48:09

Message: 11 of 18

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <josu03$s20$1@newscl01ah.mathworks.com>...
> i5, Windows7, 2012a 64 bit
> >> tic; pbug(1e9); toc
> Elapsed time is 11.442164 seconds.
> >> tic; pbug(uint32(1e9)); toc
> Elapsed time is 11.675563 seconds.
- - - - - - - - -
  Has anyone else run it exactly the way Hao did with int32(2e9) vs double(2e9) for N? Also Bruno's results with half that number of iterations and uint32 were slower per iteration than both those figures quoted by Hao, so the respective systems may differ in some significant way.

Roger Stafford

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Bruno Luong

Date: 15 May, 2012 16:19:07

Message: 12 of 18

Roger:
> - - - - - - - - -
> Has anyone else run it exactly the way Hao did with int32(2e9) vs double(2e9) for N?

Here we go:

>> tic; pbug(double(2e9)); toc
Elapsed time is 14.880369 seconds.
>> tic; pbug(uint32(2e9)); toc
Elapsed time is 14.880038 seconds.
>>

On intel i7, Windows7, Matlab 2012A 64 bit.

Bruno
 

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Roger Stafford

Date: 15 May, 2012 17:10:07

Message: 13 of 18

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jotvlq$m30$1@newscl01ah.mathworks.com>...
> >> tic; pbug(double(2e9)); toc
> Elapsed time is 14.880369 seconds.
> >> tic; pbug(uint32(2e9)); toc
> Elapsed time is 14.880038 seconds.
> On intel i7, Windows7, Matlab 2012A 64 bit.
- - - - - - - - - -
  One more suggestion and then I'll shut up, Bruno. Please try it with int32 and 2e9 rather than uint32. It is within the capacity of int32 to go as high as 2e9 and it would be interesting to see if the system responds differently with twos complement format as opposed to unsigned format.

  Your timings for 2e9 are far less than twice the times for 1e9. Did you run them on different computers (i7 vs i5)?

  Side note: I can't help wondering why Hao chose such an enormous number of iterations when the sequence of p values settles down to an unvarying repeating cycle after only about 232 iterations in this bifurcation procedure. No new numbers emerge after that. Cutting down the number of iterations would be the very best way to save computation time.

Roger Stafford

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Bruno Luong

Date: 15 May, 2012 17:20:07

Message: 14 of 18

"Roger Stafford" wrote in message <jou2lf$6bg$1@newscl01ah.mathworks.com>...

> One more suggestion and then I'll shut up, Bruno. Please try it with int32 and 2e9 rather than uint32. It is within the capacity of int32 to go as high as 2e9 and it would be interesting to see if the system responds differently with twos complement format as opposed to unsigned format.

Right on Roger, the difference is come from signed/unsigned:

>> tic; pbug(uint32(2e9)); toc
Elapsed time is 14.879072 seconds.
>> tic; pbug(int32(2e9)); toc
Elapsed time is 11.826517 seconds.
>>


>
> Your timings for 2e9 are far less than twice the times for 1e9. Did you run them on different computers (i7 vs i5)?

Yes, my desktop at work (i7) vs my laptop at home (i5).

Bruno

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Roger Stafford

Date: 15 May, 2012 17:52:06

Message: 15 of 18

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jou387$939$1@newscl01ah.mathworks.com>...
> >> tic; pbug(uint32(2e9)); toc
> Elapsed time is 14.879072 seconds.
> >> tic; pbug(int32(2e9)); toc
> Elapsed time is 11.826517 seconds.
- - - - - - - - - -
  Congratulations, Hao! Bruno's findings verify that you have found a way of speeding up certain for-loops by using the int32 format rather than double or uint32 for the for-loop indexing variable. It would be interesting to find out what it is about this difference that has such an appreciable effect on the timing.

Roger Stafford

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Bruno Luong

Date: 15 May, 2012 18:45:06

Message: 16 of 18

"Roger Stafford" wrote in message <jou546$hsa$1@newscl01ah.mathworks.com>...
>. It would be interesting to find out what it is about this difference that has such an appreciable effect on the timing.

I'm also disturbed by this result. The only explanation is that Matlab is probably doing some kind of systematic cast to signed integer in a "standard" for-loop.

I do not thing the Intel processor would make any favor between signed and unsigned integer one way or another. But may be I'm wrong about it.

In any case, I personally will not use this strange effect for little gain tha could provide in my code; and I'm not even talking about the maintainability and clarity of coding.

Bruno

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Roger Stafford

Date: 15 May, 2012 19:43:09

Message: 17 of 18

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jou87i$2lm$1@newscl01ah.mathworks.com>...
> I'm also disturbed by this result. The only explanation is that Matlab is probably doing some kind of systematic cast to signed integer in a "standard" for-loop.
>
> I do not thing the Intel processor would make any favor between signed and unsigned integer one way or another. But may be I'm wrong about it.
>
> In any case, I personally will not use this strange effect for little gain tha could provide in my code; and I'm not even talking about the maintainability and clarity of coding.
>
> Bruno
- - - - - - - -
  I share your feeling that something is amiss here, probably in Mathworks' compiler. We shouldn't have to declare type 'int32' to get full efficiency in a for-loop.

Roger Stafford

Subject: 30% faster for-loop when changing loop variable from default(double) to int

From: Oleg Melnikov

Date: 27 Nov, 2013 20:03:06

Message: 18 of 18

@Hao: Double is an accelerated data type according to [this source][1].
These [slides on optimization][2] are also helpful.
I'm not sure how the first function allocates its variables.


  [1]: http://nf.nci.org.au/facilities/software/Matlab/techdoc/matlab_prog/ch7_per6.html
  [2]: http://www.scottgorlin.com/wp-content/uploads/2008/01/day2.pdf

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