POV-Ray : Newsgroups : povray.text.tutorials : Tips & tricks: How speed-up calculations in loops - part II Server Time
12 Dec 2024 17:51:24 EST (-0500)
  Tips & tricks: How speed-up calculations in loops - part II (Message 1 to 10 of 13)  
Goto Latest 10 Messages Next 3 Messages >>>
From:
Subject: Tips & tricks: How speed-up calculations in loops - part II
Date: 11 Feb 2002 06: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
about loop indexing.

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).

Any comments ?

ABX


Post a reply to this message

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>

: Any comments ?

  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 -


Post a reply to this message

From:
Subject: Re: Tips & tricks: How speed-up calculations in loops - part II
Date: 11 Feb 2002 07:25:17
Message: <9sdf6uoabgmnh43bd3ae49mtp30qm5in3j@4ax.com>
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 ;-)

ABX


Post a reply to this message

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>

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

PS: Given that there is nothing in your loop, what about

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

Instead ?

-- 
Non Sine Numine
http://grimbert.cjb.net/
Puis, s'il advient d'un peu triompher, par hasard,






Post a reply to this message

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>

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


Post a reply to this message

From:
Subject: Re: Tips & tricks: How speed-up calculations in loops - part II
Date: 11 Feb 2002 07:44:44
Message: <gpef6u0rjkhlb88lejqmdrhtc5eebir1pt@4ax.com>

<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;
> Instead ?

What about one line version: 

#local C=1e6;

instead ?

ABX


Post a reply to this message

From:
Subject: Re: Tips & tricks: How speed-up calculations in loops - part II
Date: 11 Feb 2002 07:49:54
Message: <l8ff6u4vedkshmuu7og3nvupc0jrj9ulva@4ax.com>
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.

Oh, I misunderstand your answer.

>  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


Post a reply to this message

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


Post a reply to this message

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


Post a reply to this message

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


Post a reply to this message

Goto Latest 10 Messages Next 3 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.