POV-Ray : Newsgroups : povray.general : #declaring pre-made macro for identical repeated use? Server Time
30 Jul 2024 04:21:17 EDT (-0400)
  #declaring pre-made macro for identical repeated use? (Message 1 to 6 of 6)  
From: Kenneth
Subject: #declaring pre-made macro for identical repeated use?
Date: 17 Dec 2009 11:00:00
Message: <web.4b2a53b4201783d65f302820@news.povray.org>
Hi all.

Let's say I've made a macro, and that I use it in the usual way in my scene (by
invoking it numerous times with different variables.)  But let's say I also need
to invoke the same macro hundreds of times *without* changing the variables (in
a #while loop for example.) Forget for the moment what the macro actually does;
I have a question about what would be the most computationally-efficient way to
invoke the macro in such a case.

Here's the way I would normally do it (this is a very simplistic example, just
to illustrate the idea):

#declare counter = 1;
#while(counter <= 200)
my_macro(1,2,3) // the same variables each time
#declare counter = counter + 1;
#end

Would it be more efficient to instead pre-#declare the macro with those set
variables, before using it in the #while loop? As in...

#declare my_identical_macro = macro(1,2,3);
Then using *that* in the #while loop. (Which does work, BTW.)

When #pre-declared this way, is there just one 'instance' of the macro now
(similar to calling a pre-#declared triangle mesh hundreds of times), making the
process faster and more efficient? Or is the macro repeatedly computed anyway,
like it normally would be?

Ken


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: #declaring pre-made macro for identical repeated use?
Date: 17 Dec 2009 13:05:01
Message: <web.4b2a71b230d097eaa049a19e0@news.povray.org>
"Kenneth" <kdw### [at] earthlinknet> wrote:
> Hi all.
>
> Let's say I've made a macro, and that I use it in the usual way in my scene (by
> invoking it numerous times with different variables.)  But let's say I also need
> to invoke the same macro hundreds of times *without* changing the variables (in
> a #while loop for example.) Forget for the moment what the macro actually does;
> I have a question about what would be the most computationally-efficient way to
> invoke the macro in such a case.
>
> Here's the way I would normally do it (this is a very simplistic example, just
> to illustrate the idea):
>
> #declare counter = 1;
> #while(counter <= 200)
> my_macro(1,2,3) // the same variables each time
> #declare counter = counter + 1;
> #end
>
> Would it be more efficient to instead pre-#declare the macro with those set
> variables, before using it in the #while loop? As in...
>
> #declare my_identical_macro = macro(1,2,3);
> Then using *that* in the #while loop. (Which does work, BTW.)
>
> When #pre-declared this way, is there just one 'instance' of the macro now
> (similar to calling a pre-#declared triangle mesh hundreds of times), making the
> process faster and more efficient? Or is the macro repeatedly computed anyway,
> like it normally would be?


I'm sorry but I am afraid that you are misunderstanding what is happening here.

There is no such concept/feature as predeclaration of macros in POV-Ray.

What is happening above is that the values or "items" that your macro generates
are assigned to your variable "my_identical_macro".

So "my_identical_macro" is now not a macro, it is just a variable that contains
what your macro "produced".

To see that it does not work the way you think, try this:
(It does not work.)


#macro Test(A)
  #debug concat("\n", str(A, 0, -1), "\n")
#end // macro Test

Test(123)
#declare Tist = Test(456)
Tist


--
Tor Olav
http://subcube.com


Post a reply to this message

From: Thomas de Groot
Subject: Re: #declaring pre-made macro for identical repeated use?
Date: 18 Dec 2009 04:10:16
Message: <4b2b46f8$1@news.povray.org>
In my experience, whatever the macro does, if it is intended to give always 
the same output, it is always more efficient to keep either macro or its 
output out of the while loop, but inside a union for instance. Otherwise it 
would not make too much difference (except maybe if the macro is very 
large); e.g.

#declare my_output = macro();
union {
#while (counter <= 200)
business as usial
#declare counter = counter+1;
#end
my_output //whatever that represents
}//end of union


Thomas


Post a reply to this message

From: Kenneth
Subject: Re: #declaring pre-made macro for identical repeated use?
Date: 18 Dec 2009 07:20:01
Message: <web.4b2b6f1330d097ea65f302820@news.povray.org>
"Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
>
> What is happening above is that the values or "items" that your macro
> generates are assigned to your variable "my_identical_macro".
>
> So "my_identical_macro" is now not a macro, it is just a variable that
> contains what your macro "produced".

Thanks, Tor, for clarifying this.  I do see what you mean, and what my mistaken
assumption was. So, to use another very simplistic example, let's say that
my_macro(A,B,C) simply produces a sphere, with the variables A,B,C denoting its
scale:

#macro my_macro(A,B,C)
sphere{0,1 scale <A,B,C>}
#end

Then I #declare that (with a particular set of variables)--and choose a
better name for it, so as not to confuse myself! :-)

#declare my_identical_sphere = my_macro(1,2,3); // no longer a macro now

So when I plug that into my #while loop, all it does is *replace* the macro with
the 'thing' that the macro produced. So in essence, it would be this:

#declare counter = 1;
#while (counter <= 200)
sphere{0,1 scale <1,2,3>} // my_identical_sphere (which is now just a
// pre-#declared 'thing')
#declare counter = counter + 1;
#end

This is actually OK-- i.e, it produces the same *results* as my original
(mistaken) assumption--and now there is no macro invoked at all (which I suppose
is also good--or rather, *efficient*, computationally-speaking.)

Please correct me if my thinking is still wrong.

Ken


Post a reply to this message

From: Kenneth
Subject: Re: #declaring pre-made macro for identical repeated use?
Date: 18 Dec 2009 07:25:00
Message: <web.4b2b73c530d097ea65f302820@news.povray.org>
"Thomas de Groot" <tDOTdegroot@interDOTnlANOTHERDOTnet> wrote:
> In my experience, whatever the macro does, if it is intended to give always
> the same output, it is always more efficient to keep either macro or its
> output out of the while loop, but inside a union for instance. Otherwise it
> would not make too much difference (except maybe if the macro is very
> large)...

Thanks, Thomas.  In light of what Tor said, I had a mistaken idea of what was
happening when #declaring a pre-made macro. It all makes sense now (hopefully!)

Ken


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: #declaring pre-made macro for identical repeated use?
Date: 18 Dec 2009 11:14:15
Message: <4b2baa57@news.povray.org>
Kenneth wrote:
> "Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
>> What is happening above is that the values or "items" that your macro
>> generates are assigned to your variable "my_identical_macro".
>>
>> So "my_identical_macro" is now not a macro, it is just a variable that
>> contains what your macro "produced".
> 
> Thanks, Tor, for clarifying this.  I do see what you mean, and what my mistaken
> assumption was. So, to use another very simplistic example, let's say that
> my_macro(A,B,C) simply produces a sphere, with the variables A,B,C denoting its
> scale:
> 
> #macro my_macro(A,B,C)
> sphere{0,1 scale <A,B,C>}
> #end
> 
> Then I #declare that (with a particular set of variables)--and choose a
> better name for it, so as not to confuse myself! :-)
> 
> #declare my_identical_sphere = my_macro(1,2,3); // no longer a macro now
> 
> So when I plug that into my #while loop, all it does is *replace* the macro with
> the 'thing' that the macro produced. So in essence, it would be this:
> 
> #declare counter = 1;
> #while (counter <= 200)
> sphere{0,1 scale <1,2,3>} // my_identical_sphere (which is now just a
> // pre-#declared 'thing')
> #declare counter = counter + 1;
> #end
> 
> This is actually OK-- i.e, it produces the same *results* as my original
> (mistaken) assumption--and now there is no macro invoked at all (which I suppose
> is also good--or rather, *efficient*, computationally-speaking.)
> 
> Please correct me if my thinking is still wrong.

It seems that you have understood it correctly now.

:)

-- 
Tor Olav
http://subcube.com


Post a reply to this message

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