POV-Ray : Newsgroups : povray.text.tutorials : Tips & tricks: How speed-up calculations in loops - part II Server Time 3 Dec 2023 22:53:07 EST (-0500)
 Tips & tricks: How speed-up calculations in loops - part II (Message 1 to 10 of 13)
 From: Subject: Tips & tricks: How speed-up calculations in loops - part II Date: 11 Feb 2002 06:39:01 Message: <8l9f6u0ourilu01bf4o6jjodpvbpp69k2n@4ax.com>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#8l9f6u0ourilu01bf4o6jjodpvbpp69k2n%404ax.com",
"headline": "Tips & tricks: How speed-up calculations in loops - part II",
"dateCreated": "2002-02-11T11:39:01+00:00",
"datePublished": "2002-02-11T11:39:01+00:00",
"author": {
"@type": "Person",
"name": ""
}
}
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 07:18:33 Message: <3c67b698@news.povray.org>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#3c67b698%40news.povray.org",
"headline": "Re: Tips & tricks: How speed-up calculations in loops - part II",
"dateCreated": "2002-02-11T12:18:33+00:00",
"datePublished": "2002-02-11T12:18:33+00:00",
"author": {
"@type": "Person",
"name": "Warp"
}
}

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: Subject: Re: Tips & tricks: How speed-up calculations in loops - part II Date: 11 Feb 2002 07:25:17 Message: <9sdf6uoabgmnh43bd3ae49mtp30qm5in3j@4ax.com>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#9sdf6uoabgmnh43bd3ae49mtp30qm5in3j%404ax.com",
"headline": "Re: Tips & tricks: How speed-up calculations in loops - part II",
"dateCreated": "2002-02-11T12:25:17+00:00",
"datePublished": "2002-02-11T12:25:17+00:00",
"author": {
"@type": "Person",
"name": ""
}
}
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 07:31:43 Message: <3C67B9B8.84AD27C9@atosorigin.com>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"headline": "Re: Tips & tricks: How speed-up calculations in loops - part II",
"dateCreated": "2002-02-11T12:31:43+00:00",
"datePublished": "2002-02-11T12:31:43+00:00",
"author": {
"@type": "Person",
"name": "J\u00e9r\u00f4me Grimbert"
}
}

>
> 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,

```
 From: Warp Subject: Re: Tips & tricks: How speed-up calculations in loops - part II Date: 11 Feb 2002 07:43:27 Message: <3c67bc6e@news.povray.org>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#3c67bc6e%40news.povray.org",
"headline": "Re: Tips & tricks: How speed-up calculations in loops - part II",
"dateCreated": "2002-02-11T12:43:27+00:00",
"datePublished": "2002-02-11T12:43:27+00:00",
"author": {
"@type": "Person",
"name": "Warp"
}
}

: 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: Subject: Re: Tips & tricks: How speed-up calculations in loops - part II Date: 11 Feb 2002 07:44:44 Message:
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#gpef6u0rjkhlb88lejqmdrhtc5eebir1pt%404ax.com",
"headline": "Re: Tips & tricks: How speed-up calculations in loops - part II",
"dateCreated": "2002-02-11T12:44:44+00:00",
"datePublished": "2002-02-11T12:44:44+00:00",
"author": {
"@type": "Person",
"name": ""
}
}

<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: Subject: Re: Tips & tricks: How speed-up calculations in loops - part II Date: 11 Feb 2002 07:49:54 Message:
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#l8ff6u4vedkshmuu7og3nvupc0jrj9ulva%404ax.com",
"headline": "Re: Tips & tricks: How speed-up calculations in loops - part II",
"dateCreated": "2002-02-11T12:49:54+00:00",
"datePublished": "2002-02-11T12:49:54+00:00",
"author": {
"@type": "Person",
"name": ""
}
}
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 16:29:51 Message: <3ceff43f@news.povray.org>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#3ceff43f%40news.povray.org",
"headline": "Re: Tips & tricks: How speed-up calculations in loops - part II",
"dateCreated": "2002-05-25T20:29:51+00:00",
"datePublished": "2002-05-25T20:29:51+00:00",
"author": {
"@type": "Person",
"name": "Jonathan Wooldridge"
}
}
> >
> >  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 16:49:41 Message: <3ceff8e4@news.povray.org>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#3ceff8e4%40news.povray.org",
"headline": "Re: Tips & tricks: How speed-up calculations in loops - part II",
"dateCreated": "2002-05-25T20:49:41+00:00",
"datePublished": "2002-05-25T20:49:41+00:00",
"author": {
"@type": "Person",
"name": "Warp"
}
}
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: 25 May 2002 21:37:37 Message: <3cf03c61\$1@news.povray.org>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#3cf03c61%241%40news.povray.org",
"headline": "Re: Tips & tricks: How speed-up calculations in loops - part II",
"dateCreated": "2002-05-26T01:37:37+00:00",
"datePublished": "2002-05-26T01:37:37+00:00",
"author": {
"@type": "Person",
"name": "Jonathan Wooldridge"
}
}
"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
```