Is returning a whole array from a Perl subroutine inefficient?

后端 未结 8 1867
野性不改
野性不改 2020-12-09 10:41

I often have a subroutine in Perl that fills an array with some information. Since I\'m also used to hacking in C++, I find myself often do it like this in Perl, using refer

8条回答
  •  孤城傲影
    2020-12-09 11:03

    I know nothing about Perl so this is a language-neutral answer.

    It is, in a sense, inefficient to copy an array from a subroutine into the calling program. The inefficiency arises in the extra memory used and the time taken to copy the data from one place to another. On the other hand, for all but the largest arrays, you might not give a damn, and might prefer to copy arrays out for elegance, cussedness or any other reason.

    The efficient solution is for the subroutine to pass the calling program the address of the array. As I say, I haven't a clue about Perl's default behaviour in this respect. But some languages provide the programmer the option to choose which approach.

提交回复
热议问题