POV-Ray : Newsgroups : povray.general : optimisation of functions - how to? Server Time
26 Nov 2024 20:44:16 EST (-0500)
  optimisation of functions - how to? (Message 1 to 10 of 10)  
From: makc
Subject: optimisation of functions - how to?
Date: 19 Jan 2010 18:35:00
Message: <web.4b563f9c3efa46efa2b7633a0@news.povray.org>
Hello all, could you please advice on following problem. For example, I have two
functions

#declare frac = function (x) {
   x - floor (x)
}

#declare menger = function (x, order) {
   floor (3*frac(x*pow(3,order))) - floor (3*frac(x*pow(3,order))/2)*2
}

In procedural language like C++ to avoid recalculation of frac/pow I would write
in 2nd function

double tmp = frac(x*pow(3,order));
return floor (3*tmp) - floor (3*tmp/2)*2;

But how do I explain this to pov?

Ok, in this particular case I could use intermediate function

#declare menger2 = function (tmp) {
   floor (3*tmp) - floor (3*tmp/2)*2
}

#declare menger = function (x, order) {
   menger2(frac(x*pow(3,order)))
}

but

1) not sure if this can be done for arbitrary combination
2) wont stacking functions like that actually slow things down

Thanks.


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: optimisation of functions - how to?
Date: 19 Jan 2010 20:30:00
Message: <web.4b565c5a6e27db37d6291f840@news.povray.org>
"makc" <mak### [at] gmailspamcom> wrote:
> Hello all, could you please advice on following problem. For example, I have two
> functions
>
> #declare frac = function (x) {
>    x - floor (x)
> }
>
> #declare menger = function (x, order) {
>    floor (3*frac(x*pow(3,order))) - floor (3*frac(x*pow(3,order))/2)*2
> }
....
> But how do I explain this to pov?
>
> Ok, in this particular case I could use intermediate function
>
> #declare menger2 = function (tmp) {
>    floor (3*tmp) - floor (3*tmp/2)*2
> }
>
> #declare menger = function (x, order) {
>    menger2(frac(x*pow(3,order)))
> }
>
> but
>
> 1) not sure if this can be done for arbitrary combination

Neither do I.

I can't remember having seen a function where this way of doing it is
not possible.

But if you or anyone else can come up with a good challenge, then
please post it.


> 2) wont stacking functions like that actually slow things down

I have mostly experienced speed-ups.
But sometimes extra functions calls is not worth simple recalculations.

--
Tor Olav
http://subcube.com


Post a reply to this message

From: makc
Subject: Re: optimisation of functions - how to?
Date: 20 Jan 2010 05:30:00
Message: <web.4b56d9dd6e27db373de567e60@news.povray.org>
"Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
> "makc" <mak### [at] gmailspamcom> wrote:
> I can't remember having seen a function where this way of doing it is
> not possible.
>
> But if you or anyone else can come up with a good challenge, then
> please post it.

Not exactly my original problem, but nevertheless major waste of cpu: the
absence of "if"-like function (or am I overlooking it?)

Simple condition (x > 0) ? func (x) : 0 can be modelled, for example, with
something like func (x) * ceil ((1 + abs(x) - abs (x - 1)) / 2), but this means
pov will calculate func (x) for every x < 0 where it is not needed.

I am actually trying to render 3D fractals with isosurface, and it works, but is
painfully slow, and I don't see anything I can do about it without recompiling
povray with added internal functions :(


Post a reply to this message

From: Thorsten Froehlich
Subject: Re: optimisation of functions - how to?
Date: 20 Jan 2010 06:34:45
Message: <4b56ea55$1@news.povray.org>
On 20.01.10 11:24, makc wrote:
> "Tor Olav Kristensen"<tor### [at] TOBEREMOVEDgmailcom>  wrote:
>> "makc"<mak### [at] gmailspamcom>  wrote:
>> I can't remember having seen a function where this way of doing it is
>> not possible.
>>
>> But if you or anyone else can come up with a good challenge, then
>> please post it.
>
> Not exactly my original problem, but nevertheless major waste of cpu: the
> absence of "if"-like function (or am I overlooking it?)

You are overlooking it: What you want is "select", which is available both 
inside and outside functions. A description of its parameters, just like 
those of all other parameters, may be found in the documentation, i.e. at 
<http://www.povray.org/documentation/view/3.6.1/228/>. It can do everything 
the ternary operator in C can do, and a bit more.

	Thorsten


Post a reply to this message

From: Thorsten Froehlich
Subject: Re: optimisation of functions - how to?
Date: 20 Jan 2010 06:45:31
Message: <4b56ecdb@news.povray.org>
On 20.01.10 00:33, makc wrote:
> Hello all, could you please advice on following problem. For example, I have two
> functions
>
> #declare frac = function (x) {
>     x - floor (x)
> }
>
> #declare menger = function (x, order) {
>     floor (3*frac(x*pow(3,order))) - floor (3*frac(x*pow(3,order))/2)*2
> }
>
> In procedural language like C++ to avoid recalculation of frac/pow I would write
> in 2nd function
>
> double tmp = frac(x*pow(3,order));
> return floor (3*tmp) - floor (3*tmp/2)*2;
>
> But how do I explain this to pov?

You don't need a second function to declare a variable. While it is probably 
the easiest to read, if you really want you can reuse the sum or prod 
functions. In the documentation, i.e. at 
<http://www.povray.org/documentation/view/3.6.1/231/> you can find a few 
examples. The trick here is of course to reuse the iteration variable.

	Thorsten


Post a reply to this message

From: makc
Subject: Re: optimisation of functions - how to?
Date: 20 Jan 2010 07:15:01
Message: <web.4b56f36f6e27db373de567e60@news.povray.org>
Thorsten Froehlich <tho### [at] trfde> wrote:
> You are overlooking it: What you want is "select"

Wow thanks, that does give some noticeable boost (and so hope to it).

Re sum/prod, can't see how that is different, except that I can put expression
as argument vs it being function body. Is it faster that way?


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: optimisation of functions - how to?
Date: 20 Jan 2010 08:30:01
Message: <web.4b57052b6e27db37d6291f840@news.povray.org>
"makc" <mak### [at] gmailspamcom> wrote:
> "Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
> > "makc" <mak### [at] gmailspamcom> wrote:
> > I can't remember having seen a function where this way of doing it is
> > not possible.
> >
> > But if you or anyone else can come up with a good challenge, then
> > please post it.
>
> Not exactly my original problem, but nevertheless major waste of cpu: the
> absence of "if"-like function (or am I overlooking it?)
>
> Simple condition (x > 0) ? func (x) : 0 can be modelled, for example, with
> something like func (x) * ceil ((1 + abs(x) - abs (x - 1)) / 2), but this means
> pov will calculate func (x) for every x < 0 where it is not needed.
>
> I am actually trying to render 3D fractals with isosurface, and it works, but is
> painfully slow, and I don't see anything I can do about it without recompiling
> povray with added internal functions :(

You might want to have a look at this post:

http://news.povray.org/povray.binaries.scene-files/message/%3Cweb.4b43d232b2edd653527a960f0%40news.povray.org%3E

http://tinyurl.com/yfsl9nt

Group: povray.binaries.scene-files
Subject: "Re: Happy 2,0,1,0 Quaternion-power 'Bulb!"
From: Tor Olav Kristensen
Date: 6th January 2010

--
Tor Olav
http://subcube.com


Post a reply to this message

From: Thorsten Froehlich
Subject: Re: optimisation of functions - how to?
Date: 20 Jan 2010 08:34:57
Message: <4b570681@news.povray.org>
On 20.01.10 13:13, makc wrote:
> Thorsten Froehlich<tho### [at] trfde>  wrote:
>> You are overlooking it: What you want is "select"
>
> Wow thanks, that does give some noticeable boost (and so hope to it).
>
> Re sum/prod, can't see how that is different, except that I can put expression
> as argument vs it being function body. Is it faster that way?

Indeed, the overhead is somewhat lower than for a function call (due to the 
internal structure of the function VM). What you need to know is that you 
can you the local variable as the end value, i.e.

sum(i, ... compute variable i ..., i, ... your function here ...)

That way the sum function iterates exactly once. This works because i is 
initialized as soon as the start value is computed, hence it can be used by 
the end value computation, which is only computed once, too.

	Thorsten


Post a reply to this message

From: htinkle
Subject: Re: optimisation of functions - how to?
Date: 20 Jan 2010 23:35:18
Message: <4b57d986@news.povray.org>
Hey makc, I'm trying to render 3d fractals with pov too but am a little 
stuck. If you want to share ideas email me!

Dave.


makc wrote:

> I am actually trying to render 3D fractals with isosurface, and it works, but is
> painfully slow, and I don't see anything I can do about it without recompiling
> povray with added internal functions :(
> 
>


Post a reply to this message

From: makc
Subject: Re: optimisation of functions - how to?
Date: 21 Jan 2010 04:05:01
Message: <web.4b58187f6e27db372af12a3a0@news.povray.org>
hti### [at] gmailcom wrote:
> Hey makc, I'm trying to render 3d fractals with pov too but am a little
> stuck. If you want to share ideas email me!
>
> Dave.
>

Hey Dave, if you want to see where I am at this point, visit
http://preview.tinyurl.com/menger4D (scroll to the bottom for my last pov file)


Post a reply to this message

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