Re: Array[] building speed
- To: mathgroup at smc.vnet.net
- Subject: [mg5242] Re: Array[] building speed
- From: News at rrze.uni-erlangen.de
- Date: Fri, 15 Nov 1996 03:33:55 -0500
- Organization: Institute for Biomedical Engineering at the FAU Erlangen
- Sender: owner-wri-mathgroup at wolfram.com
Rich Klopp wrote:
From: news at MailHub.rrze.uni-erlangen.de
To: mathgroup at smc.vnet.net
>
> I am faking a 600x400 pixel 8-bit gray-scale image by defining
>
> grayfield[x_, y_] := Floor[ 256 *
> (1/2 +
> 1/4 N[Sin[2 Pi (x/100 + y/1800)]] +
> 1/4 N[Sin[2 Pi (x/4 + y/600)]])
> ]
>
> and then evaluating
>
> grayimage = Array[
> grayfield[ #1, #2 ]&,
> {600, 400}];
>
> However, the latter takes bloody forever. (Well, how about 15 minutes on a
> Power Mac 7100.) Am I doing something wrong? I note that
> Show[]ing the DensityGraphics[] of grayimage ain't very speedy either.
>
> --
> Rich Klopp
Hi Rich!
There are several stages of improving your algorithm.
Take a look at the little benchmark below.
Here we use to much exact arithmetic:
In[1]:
grayfield[x_, y_] := Floor[ 256 *
(1/2 +
1/4 N[Sin[2 Pi (x/100 + y/1800)]] +
1/4 N[Sin[2 Pi (x/4 + y/600)]])
];
grayimage = Array[
grayfield[ #1, #2 ]&,
{60, 40}];//Timing
Out[1]:
{29.928 Second, Null}
First Step: Try to use as much real arithmetic as possible:
In[2]:
ClearAll[grayfield,grayimage];
grayfield[x_, y_] := Floor[ 256 *
(0.5 +
0.25 N[Sin[2 N[Pi] (x/100. + y/1800.)]] +
0.25 N[Sin[2 N[Pi] (x/4. + y/600.)]])
];
grayimage = Array[
grayfield[ #1, #2 ]&,
{60, 40}];//Timing
Out[2]:
{17.256 Second, Null}
This one was better but there is still place for improvement:
In[3]:
ClearAll[grayfieldcompiled,grayimage];
grayfieldcompiled= Compile[{{x,_Integer},{y,_Integer}},Floor[ 256 *
(0.5 +
0.25 N[Sin[2 N[Pi] (x 0.01 + y/1800.)]] +
0.25 N[Sin[2 N[Pi] (x 0.25 + y/600.)]])
]];
grayimage = Array[
grayfieldcompiled[ #1, #2]&,
{60,40}];//Timing
Out[3]:
{1.419 Second, Null}
That is quite fast compared to the beginning!!
I hope this helps.
--
Jens Potschadtke
Institute for biomedical engineering at the FAU Erlangen (germany).
mailto:Jens.Potschadtke at stud.uni-erlangen.de
http://www.uni-erlangen.de/~sz0438/