[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] Efficient way to reverse a string
From: |
david kerns |
Subject: |
Re: [bug-gawk] Efficient way to reverse a string |
Date: |
Wed, 24 Apr 2019 23:29:13 -0700 |
I wouldn't trust any of the times from your runs 2-N without understanding
your machines disk caching...
I work on machines with 100+ GB RAM and often find the 2nd run of any
program on a large file runs in a fraction of the time because the whole
large file (20GB) was cached in RAM
On Wed, Apr 24, 2019 at 11:21 PM Peter Brooks <address@hidden>
wrote:
> Here are the timings - 'long' is:
>
> $ wc long
>
> 65008 900009 4400044 long
>
>
> $ time (rev long >/dev/null)
>
>
> real 0m0.350s
>
> user 0m0.069s
>
> sys 0m0.087s
>
> $ time (cat long|awk '{ for(i=length;i!=0;i--)x=x substr($0,i,1);}END{print
> x}' >/dev/null)
>
>
> real 0m1.565s
>
> user 0m0.702s
>
> sys 0m0.022s
>
>
>
> In terms of system time, awk is very fast, but the overhead is high enough
> to make the overall time longer.
>
>
> On Thu, 25 Apr 2019 at 07:49, Peter Brooks <address@hidden>
> wrote:
>
> > Why not just use 'rev' - as an embedded command if you want the result in
> > awk:
> >
> > $ echo the quick brown fox jumps over the lazy dog|rev
> >
> > god yzal eht revo spmuj xof nworb kciuq eht
> >
> >
> > On Thu, 25 Apr 2019 at 05:06, Peng Yu <address@hidden> wrote:
> >
> >> Hi,
> >>
> >> The following way to reverse a string seems to be inefficient as it
> >> involves multiple concatenations.
> >>
> >>
> >>
> https://www.unix.com/shell-programming-and-scripting/223077-awk-reverse-string.html
> >>
> >> Is there a more efficient way to reverse a string? Thanks.
> >>
> >> --
> >> Regards,
> >> Peng
> >>
> >>
> >
> > --
> > Peter Brooks
> >
> >
> > Mobile: +27 82 717 6404
> > Skype: Fustbariclation
> > Twitter: Fustbariclation
> > Author Page: amazon.com/author/peter_brooks
> >
>
>
> --
> Peter Brooks
>
>
> Mobile: +27 82 717 6404
> Skype: Fustbariclation
> Twitter: Fustbariclation
> Author Page: amazon.com/author/peter_brooks
>
- [bug-gawk] Efficient way to reverse a string, Peng Yu, 2019/04/24
- Re: [bug-gawk] Efficient way to reverse a string, Peter Brooks, 2019/04/25
- Re: [bug-gawk] Efficient way to reverse a string, Peng Yu, 2019/04/25
- Re: [bug-gawk] Efficient way to reverse a string, Peter Brooks, 2019/04/25
- Re: [bug-gawk] Efficient way to reverse a string, Peng Yu, 2019/04/25
- Re: [bug-gawk] Efficient way to reverse a string, Andrew J. Schorr, 2019/04/25
- Re: [bug-gawk] Efficient way to reverse a string, arnold, 2019/04/28