POV-Ray : Newsgroups : povray.text.tutorials : Tips & tricks: How speed-up calculations in loops Server Time17 Sep 2024 09:13:08 EDT (-0400)
 Tips & tricks: How speed-up calculations in loops (Message 1 to 6 of 6)
 From: Subject: Tips & tricks: How speed-up calculations in loops Date: 28 Jan 2002 10:56:35 Message:
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#f4sa5uomgj1v158rjs159ms75midq7qm52%404ax.com",
"headline": "Tips & tricks: How speed-up calculations in loops",
"dateCreated": "2002-01-28T15:56:35+00:00",
"datePublished": "2002-01-28T15:56:35+00:00",
"author": {
"@type": "Person",
"name": ""
}
}
Please don't answer with "nothing new. I use it so long". I use it so long, too.
I just want to make benchmark and result is some kind of tips&trick thing.

I want to compare speed between macro calculation, inline calculation and
function calculation. All tests are performed with currrent (10) beta of POV-Ray
3.5 for Windows (icl) on PII 233 with 128 MB and. Here are scripts:

// SCRIPT 1 - Macro calculation

#macro Calc(X,Y)sin(X)+Y+cos(X*Y)^2+1/3#end
#local MaxX=500;
#local MaxY=500;
#local X=0;
#while(X<MaxX)
#local Y=0;
#while(Y<MaxY)
#local D=Calc(X,Y);
#local Y=Y+1;
#end
#local X=X+1;
#end

// SCRIPT 2 - inline calculation

#local MaxX=500;
#local MaxY=500;
#local X=0;
#while(X<MaxX)
#local Y=0;
#while(Y<MaxY)
#local D=sin(X)+Y+cos(X*Y)^2+1/3;
#local Y=Y+1;
#end
#local X=X+1;
#end

// SCRIPT 3 - function calculations

#local Calc=function(X,Y){sin(X)+Y+cos(X*Y)^2+1/3};
#local MaxX=500;
#local MaxY=500;
#local X=0;
#while(X<MaxX)
#local Y=0;
#while(Y<MaxY)
#local D=Calc(X,Y);
#local Y=Y+1;
#end
#local X=X+1;
#end

// RESULTS

// SCRIPT 1 - macro calculations
CPU time used: kernel 20.79 seconds, user 74.22 seconds, total 95.01 seconds

// SCRIPT 2 - inline calculations
CPU time used: kernel 9.74 seconds, user 47.34 seconds, total 57.08 seconds

// SCRIPT 3 - function calculations
CPU time used: kernel 8.95 seconds, user 37.78 seconds, total 46.74 seconds

ABX
```
 From: Warp Subject: Re: Tips & tricks: How speed-up calculations in loops Date: 28 Jan 2002 11:59:21 Message: <3c558368@news.povray.org>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#3c558368%40news.povray.org",
"headline": "Re: Tips & tricks: How speed-up calculations in loops",
"dateCreated": "2002-01-28T16:59:21+00:00",
"datePublished": "2002-01-28T16:59:21+00:00",
"author": {
"@type": "Person",
"name": "Warp"
}
}
You should also compare the speed when the macro is defined in an include
file instead of the same scene file (in theory it should be much slower).
It would also be interesting to see what happens if the function is defined
in an external include file (AFAIK there should not be speed difference, but
who knows?).

--
#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 Date: 28 Jan 2002 12:30:38 Message:
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#jh2b5u89pnm3k97g1721i2544eha4m622d%404ax.com",
"headline": "Re: Tips & tricks: How speed-up calculations in loops",
"dateCreated": "2002-01-28T17:30:38+00:00",
"datePublished": "2002-01-28T17:30:38+00:00",
"author": {
"@type": "Person",
"name": ""
}
}
On 28 Jan 2002 11:59:21 -0500, Warp <war### [at] tagpovrayorg> wrote:
>  You should also compare the speed when the macro is defined in an include
> file instead of the same scene file (in theory it should be much slower).
>  It would also be interesting to see what happens if the function is defined
> in an external include file (AFAIK there should not be speed difference, but
> who knows?).

I will do it. At this moment I fill sick and I'll be absent one week probably
(depends on doctor's decision).

ABX
```
 From: JRG Subject: Re: Tips & tricks: How speed-up calculations in loops Date: 30 Jan 2002 17:33:49 Message: <3c5874cd@news.povray.org>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#3c5874cd%40news.povray.org",
"headline": "Re: Tips & tricks: How speed-up calculations in loops",
"dateCreated": "2002-01-30T22:33:49+00:00",
"datePublished": "2002-01-30T22:33:49+00:00",
"author": {
"@type": "Person",
"name": "JRG"
}
}

> I will do it. At this moment I fill sick and I'll be absent one week probably
> (depends on doctor's decision).

Does this mean that I'll have to wait some more for those spaghetti?
Hope you feel better soon.

--
#local j=text{ttf"arial""JRG".2,0}#local J=0;#while(J<10)#local R=0;#while
(R<2)#local G=0;#while(G<1)#if(inside(j<R,G.1>))object{j scale.025translate
<R-1G-J/20J/-40+2>pigment{rgb<9J>}}#debug"O"#else#debug" "#end#local G=G+
.025;#end#local R=R+.05;#debug"\n"#end#local J=J+1;#end// JRG

Home: http://digilander.iol.it/jrgpov  //New: Kitchen scene WIP
```
 From: Subject: Re: Tips & tricks: How speed-up calculations in loops Date: 5 Feb 2002 11:32:38 Message: <6eiv5uge7iq4efs54sm4rgk24d0safdgjm@4ax.com>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#6eiv5uge7iq4efs54sm4rgk24d0safdgjm%404ax.com",
"headline": "Re: Tips & tricks: How speed-up calculations in loops",
"dateCreated": "2002-02-05T16:32:38+00:00",
"datePublished": "2002-02-05T16:32:38+00:00",
"author": {
"@type": "Person",
"name": ""
}
}

wrote:
> I want to compare speed between macro calculation, inline calculation and
> function calculation. All tests are performed with currrent (10) beta of POV-Ray
> 3.5 for Windows (icl) on PII 233 with 128 MB and. Here are scripts:

Here is result of extended test made after Warp's suggestions:

Total CPU time used was measured in seconds.
The same machine, script and calculation was used.
Every scene was started 3 times. result was averaged.
Two loops as earlier but with max. 1000 so, it was 1.000.000 use of calculation:

external macro       - 1026,47 [s]
internal macro       -  350,73 [s]
calculation on-place -  221,15 [s]
external function    -  179,62 [s]
internal function    -  178,68 [s]

ABX
```
 From: Warp Subject: Re: Tips & tricks: How speed-up calculations in loops Date: 5 Feb 2002 11:52:55 Message: <3c600de6@news.povray.org>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#3c600de6%40news.povray.org",
"headline": "Re: Tips & tricks: How speed-up calculations in loops",
"dateCreated": "2002-02-05T16:52:55+00:00",
"datePublished": "2002-02-05T16:52:55+00:00",
"author": {
"@type": "Person",
"name": "Warp"
}
}

: external macro       - 1026,47 [s]
: internal macro       -  350,73 [s]

As I thought.

This means that if you are using some #included macros in really tight
loops which are taking lots of time to parse, you'd better just copy-paste
the used macros from the include file to the beginning of the current file
to gain parsing speed.

: external function    -  179,62 [s]
: internal function    -  178,68 [s]

This was also predictable and good to know: Functions in #included files
are not slower than functions in the current file, so they can be used
without worries.

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