POV-Ray : Newsgroups : povray.text.tutorials : Tips & tricks: How speed-up calculations in loops - part II Server Time: 22 Jan 2020 07:27:06 GMT
 Tips & tricks: How speed-up calculations in loops - part II (Message 1 to 10 of 13)
 From: W³odzimierz ABX Skiba Subject: Tips & tricks: How speed-up calculations in loops - part II Date: 11 Feb 2002 11:39:01 Message: <8l9f6u0ourilu01bf4o6jjodpvbpp69k2n@4ax.com>
```Inspired by the result of previous test I've made another benchmark:
How functions speed-up simplest expressions - in this case I wondered what

I compared speed of two scripts:

//SCRIPT 1
#local dec=function(N){N-1};
#local C=1000000;
#while(C)
#local C=dec(C);
#end

and

//SCRIPT 2
#local C=1000000;
#while(C)
#local C=C-1;
#end

Note difference: first script use function to decrease value of parameter.
Both scripts were started three times on PII 233 with 128 MB (3.5 b 11).
Result: measuring CPU time first script was little faster then second about:
- kernel time: 5,64%
- user time: 11,00%
- total time: 9,38%

Then I compared the same replacement for increased index of loop.

//SCRIPT 3
#local inc=function(N){N+1};
#local C=0;
#while(C<1000000)
#local C=inc(C);
#end

and

//SCRIPT 4
#local C=0;
#while(C<1000000)
#local C=C+1;
#end

In this case there is no difference in speed. It changed from test to test. It
was around +/- 2%. Not big deal in this case.

Additional note: there was the same number of interations but version with
decreasing was faster (20-30%) than version with increasing (probably becouse
of no additional tokens in loop condition).

ABX
```
 From: Warp Subject: Re: Tips & tricks: How speed-up calculations in loops - part II Date: 11 Feb 2002 12:18:33 Message: <3c67b698@news.povray.org>
```W³odzimierz ABX Skiba <abx### [at] babilonorg> wrote:

One of the oldest tricks known, loop unrolling, should speed up as well.

--
#macro N(D)#if(D>99)cylinder{M()#local D=div(D,104);M().5,2pigment{rgb M()}}
N(D)#end#end#macro M()<mod(D,13)-6mod(div(D,13)8)-3,10>#end blob{
N(11117333955)N(4254934330)N(3900569407)N(7382340)N(3358)N(970)}//  - Warp -
```
 From: W³odzimierz ABX Skiba Subject: Re: Tips & tricks: How speed-up calculations in loops - part II Date: 11 Feb 2002 12:25:17 Message: <9sdf6uoabgmnh43bd3ae49mtp30qm5in3j@4ax.com>
```On 11 Feb 2002 07:18:33 -0500, Warp <war### [at] tagpovrayorg> wrote:
>
>  One of the oldest tricks known, loop unrolling, should speed up as well.

Nobody likes unrolling 1000000 iterations ;-)

ABX
```
 From: JÃ©rÃ´me Grimbert Subject: Re: Tips & tricks: How speed-up calculations in loops - part II Date: 11 Feb 2002 12:31:43 Message: <3C67B9B8.84AD27C9@atosorigin.com>
```"W³odzimierz ABX Skiba" wrote:
>
> On 11 Feb 2002 07:18:33 -0500, Warp <war### [at] tagpovrayorg> wrote:
> > : Any comments ?
> >
> >  One of the oldest tricks known, loop unrolling, should speed up as well.
>
> Nobody likes unrolling 1000000 iterations ;-)

But what is then the shell for ?
I can use it to generate the 1000000 iteration steps... a big file.
but that's about only 80 megs for a single step of 80 chars.

Upto 2 Giga, there should not be any problem.

#local C=0;
#local C=100000;

--
Non Sine Numine
http://grimbert.cjb.net/
Puis, s'il advient d'un peu triompher, par hasard,
Ne pas être obligé d'en rien rendre à César,
Vis-à-vis de soi-même en garder le mérite,
Bref, dédaignant d'être le lierre parasite,
Lors même qu'on n'est pas le chêne ou le tilleul,
Ne pas monter bien haut, peut-être, mais tout seul !
```
 From: Warp Subject: Re: Tips & tricks: How speed-up calculations in loops - part II Date: 11 Feb 2002 12:43:27 Message: <3c67bc6e@news.povray.org>
```W³odzimierz ABX Skiba <abx### [at] babilonorg> wrote:
: Nobody likes unrolling 1000000 iterations ;-)

That's not what loop unrolling means.
It means that you take a divisor of the number of loops to perform and
"unroll" the body of the loop that many times and decrease the loop index by
that amount.
That is, if your loop is something like:

#declare Ind = 1000000;
#while(Ind)
do_something()
#declare Ind = Ind-1;
#end

you convert it to something like:

#declare Ind = 1000000;
#while(Ind)
do_something()
do_something()
do_something()
do_something()
do_something()
do_something()
do_something()
do_something()
do_something()
do_something()
#declare Ind = Ind-10;
#end

This should parse faster.

--
#macro M(A,N,D,L)plane{-z,-9pigment{mandel L*9translate N color_map{[0rgb x]
[1rgb 9]}scale<D,D*3D>*1e3}rotate y*A*8}#end M(-3<1.206434.28623>70,7)M(
-1<.7438.1795>1,20)M(1<.77595.13699>30,20)M(3<.75923.07145>80,99)// - Warp -
```
 From: W³odzimierz ABX Skiba Subject: Re: Tips & tricks: How speed-up calculations in loops - part II Date: 11 Feb 2002 12:44:44 Message:
```On Mon, 11 Feb 2002 13:31:52 +0100, Jérôme Grimbert
<jer### [at] atosorigincom> wrote:
> > Nobody likes unrolling 1000000 iterations ;-)
>
> But what is then the shell for ?

It was general experiment to show:
"if you have to use loop then use it this way becouse it's faster"

If there is no need to use loop then of course
this experiment has nothing to do.

I hope there are other users who appreciate result of this experiment.

> PS: Given that there is nothing in your loop, what about
> #local C=0;
> #local C=100000;

#local C=1e6;

ABX
```
 From: W³odzimierz ABX Skiba Subject: Re: Tips & tricks: How speed-up calculations in loops - part II Date: 11 Feb 2002 12:49:54 Message:
```On 11 Feb 2002 07:43:27 -0500, Warp <war### [at] tagpovrayorg> wrote:
> : Nobody likes unrolling 1000000 iterations ;-)
>
>  That's not what loop unrolling means.

>  It means that you take a divisor of the number of loops to perform and
> "unroll" the body of the loop that many times and decrease the loop index by
> that amount.

But this can still cooperate with both my advises.

ABX
```
 From: Jonathan Wooldridge Subject: Re: Tips & tricks: How speed-up calculations in loops - part II Date: 25 May 2002 20:29:51 Message: <3ceff43f@news.povray.org>
```> >
> >  One of the oldest tricks known, loop unrolling, should speed up as
well.
>
> Nobody likes unrolling 1000000 iterations ;-)
>

Loop unrolling isn't about coding every iteration, for that very reason.
Doing an unroll of a loop reduces the number of passes for the loop.

If you have a million iterations, then there is some overhead for each of a
million passes through the loop.
You can reduce this by a factor of ten, by unrolling the loop ten times
within the loop.

Try timing this:

// Unrolled Script
#local C=1000000;
#while(C)
#local C=C-1;
// Do stuff with C

#local C=C-1;
// Do stuff with C

#local C=C-1;
// Do stuff with C

#local C=C-1;
// Do stuff with C

#local C=C-1;
// Do stuff with C

#local C=C-1;
// Do stuff with C

#local C=C-1;
// Do stuff with C

#local C=C-1;
// Do stuff with C

#local C=C-1;
// Do stuff with C

#local C=C-1;
// Do stuff with C

#end
```
 From: Warp Subject: Re: Tips & tricks: How speed-up calculations in loops - part II Date: 25 May 2002 20:49:41 Message: <3ceff8e4@news.povray.org>
```Jonathan Wooldridge <jwo### [at] attbicom> wrote:
> #local C=C-1;
> // Do stuff with C

> #local C=C-1;
> // Do stuff with C

If you can get rid of those #locals and do only one #local C=C-10 at the
end of the loop, better.

--
#macro N(D)#if(D>99)cylinder{M()#local D=div(D,104);M().5,2pigment{rgb M()}}
N(D)#end#end#macro M()<mod(D,13)-6mod(div(D,13)8)-3,10>#end blob{
N(11117333955)N(4254934330)N(3900569407)N(7382340)N(3358)N(970)}//  - Warp -
```
 From: Jonathan Wooldridge Subject: Re: Tips & tricks: How speed-up calculations in loops - part II Date: 26 May 2002 01:37:37 Message: <3cf03c61\$1@news.povray.org>
```"Warp" <war### [at] tagpovrayorg> wrote in message
news:3ceff8e4@news.povray.org...
> Jonathan Wooldridge <jwo### [at] attbicom> wrote:
> > #local C=C-1;
> > // Do stuff with C
>
> > #local C=C-1;
> > // Do stuff with C
>
>   If you can get rid of those #locals and do only one #local C=C-10 at the
> end of the loop, better.
>

No sooner said than done!

// Script without so many locals ("Spam, spam, spam and eggs spam hasn't got
too much spam in it"...)
#local C=1000000;
#while(C)
do_stuff( C );
do_stuff( C - 1 );
do_stuff( C - 2 );
...
do_stuff( C - 8 );
do_stuff( C - 9 );
do_stuff( C - 10 );

#local C = C - 10;
#end
```