bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] Efficient way to reverse a string


From: Peter Brooks
Subject: Re: [bug-gawk] Efficient way to reverse a string
Date: Thu, 25 Apr 2019 08:19:30 +0200

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


reply via email to

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