### Tags: double, instead, ittakes, matlab, optimizing, programming, running, simulate, single, solve, speed, time, upthe

# Using single instead of double

On Programmer » Matlab

7,916 words with 9 Comments; publish: Wed, 07 May 2008 21:18:00 GMT; (20077.64, « »)

Hi,

I got an optimizing problem that I'm running Matlab to solve. It

takes a long time to simulate so I'm trying to find ways to speed up

the simulation time. Since I have no use of the extra accuracy using

double floats (64bits) I would like to use single floats (32bits)

instead. Is it possible to change the settings in Matlab so all

variables will be in single floats instead of double as default?

Thanks,

//Chris

*http://matlab.todaysummary.com/q_matlab_62449.html*

All Comments

Leave a comment...

- 9 Comments
- 'help datatypes' will give you a summary of the different formats you
can use. Also, 'help uint' or 'help uint32' might give you some more

information

#1; Wed, 07 May 2008 21:19:00 GMT

- 'help datatypes' will give you a summary of the different formats you
- Chris wrote:
>

> Hi,

> I got an optimizing problem that I'm running Matlab to solve. It

> takes a long time to simulate so I'm trying to find ways to speed

> up

> the simulation time. Since I have no use of the extra accuracy

> using

> double floats (64bits) I would like to use single floats (32bits)

> instead. Is it possible to change the settings in Matlab so all

> variables will be in single floats instead of double as default?

> Thanks,

> //Chris

But changing the format doesn't change how the computation is done. I

want to speed up the computation time by using single variables in

all operations instead of double. It seems that even though I change

to single I don't gain anything in computation time for matrix

operations, I only gain computation time if I run operations with

non-matrix single data. Does Matlab not support matrix operations for

single floats?

#2; Wed, 07 May 2008 21:20:00 GMT

- Chris wrote:
- Yes you are right that the calculation method remains unchanged. As
far as I know you can't do anything about that. The benefit of

changing the format though is that your variables will take up less

space in memory. For big or long calculations this might mean you

will prevent the need for virtual memory. It also means that

operations that require matlab to make a copy of some data will be

processed faster.

#3; Wed, 07 May 2008 21:21:00 GMT

- Yes you are right that the calculation method remains unchanged. As
- Dan Pearce wrote:
>

> Yes you are right that the calculation method remains unchanged.

> As

> far as I know you can't do anything about that. The benefit of

> changing the format though is that your variables will take up less

> space in memory. For big or long calculations this might mean you

> will prevent the need for virtual memory. It also means that

> operations that require matlab to make a copy of some data will be

> processed faster.

Check this out though:

Name Size Bytes Class

a 1000x1000 8000000 double array

b 1000x1000 8000000 double array

c 1000x1000 4000000 single array

d 1000x1000 4000000 single array

Grand total is 4000000 elements using 24000000 bytes

Elapsed time is 2.283729 seconds.

Elapsed time is 0.591829 seconds.

#4; Wed, 07 May 2008 21:23:00 GMT

- Dan Pearce wrote:
- Steve Amphlett wrote:
>

> Dan Pearce wrote:

unchanged.

> less

you

> be

> Check this out though:

>

> Name Size Bytes Class

> a 1000x1000 8000000 double array

> b 1000x1000 8000000 double array

> c 1000x1000 4000000 single array

> d 1000x1000 4000000 single array

> Grand total is 4000000 elements using 24000000 bytes

>

> Elapsed time is 2.283729 seconds.

> Elapsed time is 0.591829 seconds.

Hmm.. if I run that test I get:

Name Size Bytes Class

a 1000x1000 4000000 single array

b 1000x1000 4000000 single array

c 1000x1000 8000000 double array

d 1000x1000 8000000 double array

Grand total is 4000000 elements using 24000000 bytes

Elapsed time is 1.188000 seconds.

Elapsed time is 1.407000 seconds.

weird...

At the moment I'm running format in short mode. I would like to

decrease number of digits to less thatn 5 but it seems that it's not

possible to decrease the number of digits, all I can choose from is

format short or long.

#5; Wed, 07 May 2008 21:24:00 GMT

- Steve Amphlett wrote:
- Chris <ekur.matlab.todaysummary.com.nofunatall.com> writes:
> Hi,

> I got an optimizing problem that I'm running Matlab to solve. It

> takes a long time to simulate so I'm trying to find ways to speed up

> the simulation time. Since I have no use of the extra accuracy using

> double floats (64bits) I would like to use single floats (32bits)

> instead. Is it possible to change the settings in Matlab so all

> variables will be in single floats instead of double as default?

Today's CPUs compute double-precision results almost as fast (or just

as fast) as single-precision. The only real speed benefit is reduced

memory bandwidth, which will only be an issue in some applications.

I would suggest looking into other ways to optimize. Start with the

MATLAB profiler to determine the bottleneck.

Peter Boettcher <boettcher.matlab.todaysummary.com.ll.mit.edu>

MIT Lincoln Laboratory

MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

#6; Wed, 07 May 2008 21:25:00 GMT

- Chris <ekur.matlab.todaysummary.com.nofunatall.com> writes:
- > At the moment I'm running format in short mode.
short only changes the number of digits that are displayed. It

doesn't change way the size of nature of the stored data.

> Elapsed time is 1.188000 seconds.

> Elapsed time is 1.407000 seconds.

The times will be system specific. There are several possible reasons

for your above times. If you don't have a lot of RAM you might have

needed to switch briefly to virtual memory? Try clearing a and b from

the work space between runs, so

tic;b=a*a;

timeOne = toc

clear a b

tic;d=c*c;

timeTwo = toc

#7; Wed, 07 May 2008 21:26:00 GMT

- > At the moment I'm running format in short mode.
- Dan Pearce wrote:
>

> short only changes the number of digits that are displayed. It

> doesn't change way the size of nature of the stored data.

>

> The times will be system specific. There are several possible

> reasons

> for your above times. If you don't have a lot of RAM you might have

> needed to switch briefly to virtual memory? Try clearing a and b

> from

> the work space between runs, so

> tic;b=a*a;

> timeOne = toc

> clear a b

> tic;d=c*c;

> timeTwo = toc

You'll also find that the results on the first attempt will differ

wildly from those of the second. I suppose it's because the LHS

matrices already exist after the first time.

If I shrink my matrices down to 200x200 I repeatably get this after a

few goes:

Elapsed time is 0.011680 seconds.

Elapsed time is 0.006556 seconds.

#8; Wed, 07 May 2008 21:27:00 GMT

- Dan Pearce wrote:
- Steve Amphlett wrote:
>

> Dan Pearce wrote:

> have

b

> You'll also find that the results on the first attempt will differ

> wildly from those of the second. I suppose it's because the LHS

> matrices already exist after the first time.

> If I shrink my matrices down to 200x200 I repeatably get this after

> a

> few goes:

>

> Elapsed time is 0.011680 seconds.

> Elapsed time is 0.006556 seconds.

Ok thanks for the help. I've used the profiler to remove some

bottlenecks and also checked other pages for speeding up Matlab.

Still one simulation takes about 8hours so I would like to decrease

the simulation time even more. And I thought changing to single would

speed about about 50% since the number of bits per variable is half

of a double. But if the computation speed is about the same for both

single and double then I understand there is nothing to gain except

from the mentioned thing about storing in RAM instead of in the

virtual memory.

#9; Wed, 07 May 2008 21:28:00 GMT

- Steve Amphlett wrote: