Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!cunixf.cc.columbia.edu!ben From: ben@cunixf.cc.columbia.edu (Ben Fried) Newsgroups: comp.lang.perl Subject: Why is this so slow? Message-ID: <1990Dec6.194412.9132@cunixf.cc.columbia.edu> Date: 6 Dec 90 19:44:12 GMT Sender: ben@cunixf.cc.columbia.edu (Ben Fried) Organization: Columbia University Lines: 61 I've written a filter in perl to do printer page counting for DEC ln03 printers. Right now it seems to be working fine, but it runs abysmally slowly. The vast majority of time is taken up in the following loop: mainloop: while(read(STDIN, $_, 2048)) { foreach $i ($[..length($_)) { $num_to_print = $i; $oc=$c; $c = substr($_, $i, 1); if ($tektronixmode) { if (($c eq "\f") && ($oc eq "\033")) { $pageno++; } } else { if ($ascii_mode) { if ($c eq "\n") { $lineno++; if ($lineno == $lines_per_page) { $lineno = 0; $pageno++; } } } else { if ($c eq "\f") { $lineno = 0; $pageno++; } } } if ($pageno >= $max_pages) { $debug && print STDERR "aborted at page $pageno\n"; print STDOUT substr($_, $[, $num_to_print); last mainloop; } } print STDOUT; } Can anyone give me some pointers to speed this up? I already tried using ord to cast $c and $oc to numbers, and using integer comparisons agains the ords of \n, \f, and \033, but that actually ran slower than the above code. All that really matters is that I be able to count the number of pages on the input stream, and be able to break out of the loop after either (a) EOF on stdin, or (b) $pageno == $max_pages, whichever comes first. I was thinking of doing a split(//), and then using grep to count occurrences, but couldn't think of a good way of stopping when $pageno hits $max_pages. Now that I think about it, maybe I should be able to do this, I just have to come up with a more complicated EXPR for grep. Perhaps it's time I went through the JAPH collection more closely. Still, would splitting input into arrays and using grep be any faster? Ben -- Benjamin Fried ben@cunixf.cc.columbia.edu rutgers!columbia!ben Brought to you by Super Global Mega Corp .com