Why does Perl's tr/\n// get slower and slower as line lengths increase?

前端 未结 3 1283
慢半拍i
慢半拍i 2021-02-07 20:25

In perlfaq5, there\'s an answer for How do I count the number of lines in a file?. The current answer suggests a sysread and a tr/\\n//. I wanted to tr

3条回答
  •  名媛妹妹
    2021-02-07 20:35

    Long lines are about 65 times larger than short lines, and your numbers indicate that tr/\n// runs exactly 65 times slower. This is as expected.

    wc evidently scales better for long lines. I don't really know why; perhaps because it is tuned to just count newlines, especially when you use the -l option.

提交回复
热议问题