Is returning a whole array from a Perl subroutine inefficient?

后端 未结 8 1865
野性不改
野性不改 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 10:58

    3 other potentially LARGE performance improvements if you are reading an entire, largish file and slicing it into an array:

    1. Turn off BUFFERING with sysread() instead of read() (manual warns about mixing)
    2. Pre-extend the array by valuing the last element - saves memory allocations
    3. Use Unpack() to swiftly split data like uint16_t graphics channel data

    Passing an array ref to the function allows the main program to deal with a simple array while the write-once-and-forget worker function uses the more complicated "$@" and arrow ->[$II] access forms. Being quite C'ish, it is likely to be fast!

提交回复
热议问题