Csound Csound-dev Csound-tekno Search About

[Csnd] Recursive UDOs, order of execution

Date2019-10-02 08:54
FromBernard Geyer
Subject[Csnd] Recursive UDOs, order of execution
In all examples concerning recursive UDOs, I see the same way of programing,
where all instances are first put on the stack, and the effective code of
the UDO is executed later, in reverse order:

opcode Recurse1, 0, io
	iN, icnt xin
	if icnt >= iN-1 igoto cont
	Recurse1 iN, icnt+1
cont:
	printf_i "%d\n", 1, icnt
endop

I would rather write it this way:

opcode Recurse2, 0, io
	iN, icnt xin
	printf_i "%d\n", 1, icnt

	if icnt < iN-1 then
		Recurse2 iN, icnt+1
	endif
endop

To test both:
instr 1
printf_i "\Recurse 1:\n", 1
Recurse1 5
printf_i "Recurse 2:\n", 1
Recurse2 5
endin

Is there a reason the prefer the first way ? Is it more efficient ? Has it
to do with tail optimization ?

Best,
Bernard




--
Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2019-10-02 10:06
FromRory Walsh
SubjectRe: [Csnd] Recursive UDOs, order of execution
Personally I prefer the second way because I find it more readable. I think the reason why so many of the recursive UDOs follow the first route is that most of the early examples were published by the same composer. I don't know if one is more efficient than the other, but if so I imagine the difference is minimal. You can always run with -v to see exactly what's happening under the hood. 

On Wed, 2 Oct 2019 at 08:46, Bernard Geyer <micamusic2@gmail.com> wrote:
In all examples concerning recursive UDOs, I see the same way of programing,
where all instances are first put on the stack, and the effective code of
the UDO is executed later, in reverse order:

opcode Recurse1, 0, io
        iN, icnt xin
        if icnt >= iN-1 igoto cont
        Recurse1 iN, icnt+1
cont:
        printf_i "%d\n", 1, icnt
endop

I would rather write it this way:

opcode Recurse2, 0, io
        iN, icnt xin
        printf_i "%d\n", 1, icnt

        if icnt < iN-1 then
                Recurse2 iN, icnt+1
        endif
endop

To test both:
instr 1
printf_i "\Recurse 1:\n", 1
Recurse1 5
printf_i "Recurse 2:\n", 1
Recurse2 5
endin

Is there a reason the prefer the first way ? Is it more efficient ? Has it
to do with tail optimization ?

Best,
Bernard




--
Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2019-10-02 18:03
Fromjoachim heintz
SubjectRe: [Csnd] Recursive UDOs, order of execution
i also prefer this, and i used it in my example 03G08 in the floss manual:
http://write.flossmanuals.net/csound/g-user-defined-opcodes/

	joachim



On 02/10/19 11:06, Rory Walsh wrote:
> Personally I prefer the second way because I find it more readable. I
> think the reason why so many of the recursive UDOs follow the first
> route is that most of the early examples were published by the same
> composer. I don't know if one is more efficient than the other, but if
> so I imagine the difference is minimal. You can always run with -v to
> see exactly what's happening under the hood.
>
> On Wed, 2 Oct 2019 at 08:46, Bernard Geyer  > wrote:
>
>     In all examples concerning recursive UDOs, I see the same way of
>     programing,
>     where all instances are first put on the stack, and the effective
>     code of
>     the UDO is executed later, in reverse order:
>
>     opcode Recurse1, 0, io
>             iN, icnt xin
>             if icnt >= iN-1 igoto cont
>             Recurse1 iN, icnt+1
>     cont:
>             printf_i "%d\n", 1, icnt
>     endop
>
>     I would rather write it this way:
>
>     opcode Recurse2, 0, io
>             iN, icnt xin
>             printf_i "%d\n", 1, icnt
>
>             if icnt < iN-1 then
>                     Recurse2 iN, icnt+1
>             endif
>     endop
>
>     To test both:
>     instr 1
>     printf_i "\Recurse 1:\n", 1
>     Recurse1 5
>     printf_i "Recurse 2:\n", 1
>     Recurse2 5
>     endin
>
>     Is there a reason the prefer the first way ? Is it more efficient ?
>     Has it
>     to do with tail optimization ?
>
>     Best,
>     Bernard
>
>
>
>
>     --
>     Sent from:
>     http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
>
>     Csound mailing list
>     Csound@listserv.heanet.ie 
>     https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
>     Send bugs reports to
>             https://github.com/csound/csound/issues
>     Discussions of bugs and features can be posted here
>
> Csound mailing list Csound@listserv.heanet.ie
> 
> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to
> https://github.com/csound/csound/issues Discussions of bugs and features
> can be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here