in reply to Using Perl saves time....

Why did you use -l for the ls? This certainly made it slower because it had to stat every file it found. Also ls could be aliased in your shell to something funky (like one that uses different colours for differerent file types) so for a fair comparison do
/bin/ls | wc -l

Replies are listed 'Best First'.
Re^2: Using Perl saves time....
by merlyn (Sage) on Jul 18, 2005 at 11:10 UTC
    In Using Perl saves time....:
    ls -1 | wc
    In Re: Using Perl saves time....:
    Why did you use -l for the ls?
    I think your computer is not showing you the difference between a digit one and the letter ell. The original post is an (unnecessary) digit one. You're complaining rightly about the expense of a letter ell long listing (if that was indeed the case).

    It's unnecessary because ls has two behaviors, depending on whether the output is a terminal or not (something I count as being broken, but oh well). To a terminal, it columnizes, but to a pipe or file, it's automatically one element per line (classic mode). Thank the idiots at Bezerkley for this abomination. This leads people to believe that they need to add "-1" to get one column, when in fact that's usually not necessary.

    As an example, compare "ls" with "ls | cat".

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Oh, you are right I did not have to use -1 (one) as ls|wc counts the same as ls -1|wc

      You learn something every time...

      I didn't even think of the -1 (one) option, I thought he was using -l to make sure it was one line per file or something.

      Still it's odd that it's so slow, maybe AIX is just nasty.

      ... Thank the idiots at Bezerkley ...

      It's a joke, right?

      Dodge This!
        What part of it is a joke? "ls" worked fine coming out of Bell Labs. Then one of the BSD releases out of Berkeley added this behavior. Thus, it's a Berkeley (aka Bezerkley when they do something weird) product.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re^2: Using Perl saves time....
by jhourcle (Prior) on Jul 18, 2005 at 11:17 UTC

    He used -1 which gives you one item per line, not -l which stats the files

    Of course, under most OSes, when you pipe ls into something, it implies ls -1, so it streams faster, as opposed to trying to determine how many columns to build. (I've not used AIX, so it's possible that it doesn't do this).

    I would, however, recommend using the -l flag to wc, so that it only needs to count the lines, and not the words and characters as well.

    And most ufs systems choke hard on ls when you have too many items in a directory. I saw a poorly set up system, that was rolling its log files each minute (it might've been one per transaction). The backups were failing, because it took more than 24 hrs for it to generate the listing of the directory with more than 2 million entries ... so the next backup would start running before the first one had finished.

Re^2: Using Perl saves time....
by Smylers (Pilgrim) on Jul 18, 2005 at 11:12 UTC
    Why did you use -l for the ls?

    He didn't use -l; he used -1, so as to list just 1 file per line, to make the counting right. Though at least some versions of ls spot when their output is being piped and infer -1 anyway.

    Smylers