coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] seq: speed up the common case by 25x


From: Jim Meyering
Subject: Re: [PATCH] seq: speed up the common case by 25x
Date: Fri, 01 Apr 2011 12:29:08 +0200

Jim Meyering wrote:
...
> Yeah, yeah, I should just replace it with a function
> that runs the pipeline when there's a single number....
> Ok.  Now I'll use this:
>
> seq()
> {
>   case $#:$1 in 1:[0-9]*) yes | head -n"$1" | cat -n | tr -cd '[0-9\n]';;
>     *) env seq "$@";; esac
> }
>
> It can generate half a billion numbers in 10 seconds:
>
>     $ time seq 500000000 |tail -3
>     500000000
>     seq 500000000  25.98s user 5.08s system 305% cpu 10.174 total
>     tail  4.92s user 1.62s system 64% cpu 10.174 total

It doesn't change the output, but it's better to avoid
the use of [] above.  If there had been brackets in
the input, that use of GNU tr would not have removed them.
Here's a fixed version:

seq()
{
  # Up to ~25 times faster than seq-8.10 for large N.
  case $#:$1 in
    1:[0-9]*) yes | head -n"$1" | cat -n | tr -cd '0-9\n';;
    *) env seq "$@";;
  esac
}



reply via email to

[Prev in Thread] Current Thread [Next in Thread]