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 10:26:20 +0200

Actually, though, the awk version is considerably faster than calling 'rev'
from awk - even though slower than 'rev' itself. Here are some timings:

$./test_time.sh
awk_rev:  min: 0.024 max: 0.030 mean: 0.02496 std: 0.00096871
pure_rev:  min: 0.004 max: 0.005 mean: 0.00406 std: 0.000237487
pure_awk:  min: 0.003 max: 0.004 mean: 0.00301 std: 9.94987e-05




Timing script ( test_time.sh ):

#!/bin/bash
awk_script=$(mktemp /tmp/awk_script.XXXX)
cat >$awk_script <<"@@END"
BEGIN {
min=9999999999999999999999;
max=-min;
}
    {
    if ($0 < min ) min = $0;
    if ($0 > max ) max = $0;
    count++;
    sum+=$0;
    sq_sum += $0*$0;
    }
END {
    if ( min == max )
{
mean = min;
std = 0;
variance=0;
}
    else
{
mean = sum / count;
variance = sq_sum / count - mean * mean;
std = sqrt(variance);
}
    print FILENAME ":  min: " min " max: " max " mean: " mean " std: " std;
    }
@@END
rm -f awk_rev pure_rev pure_awk
for i in {1..100}
do
(time (cat short|awk ' {rev="echo " "\""$0"\"" "|rev";rev|getline
backwards;close(rev);print backwards;}' - >/dev/null) ) 2>&1| grep sys|sed
's/sys //g'|sed 's/0m//'|sed 's/s//' >>awk_rev
(time (cat short|rev >/dev/null)  )2>&1| grep sys|sed 's/sys //g'|sed
's/0m//'|sed 's/s//' >>pure_rev
(time (echo short | awk '{ for(i=length;i!=0;i--)x=x
substr($0,i,1);}END{print x}' >/dev/null) ) 2>&1| grep sys|sed 's/sys //g'|sed
's/0m//'|sed 's/s//' >>pure_awk
done
for i in awk_rev pure_rev pure_awk
    do
    awk -f $awk_script $i
    done
rm awk_rev pure_rev pure_awk
rm $awk_script









On Thu, 25 Apr 2019 at 09:28, Peter Brooks <address@hidden>
wrote:

> Yes, using it in awk is simple:
>
> echo "The quick brown fox jumps over the lazy dog"|awk ' {rev="echo "
> "\""$0"\"" "|rev";rev|getline backwards;close(rev);print backwards;}' -
>
>
>
>
> On Thu, 25 Apr 2019 at 08:21, david kerns <address@hidden> wrote:
>
>> I think he's asking in the context of an awk program...
>>
>> I don't think you can get away from concat w/out using printf on each
>> char, maybe buffering will save you, but intuitively, I would expect better
>> performance out of concat on a string with one print at the end...
>>
>> $ echo abcdefg 123 456 ABC | gawk '{n=split($0, a, "");for
>> (i=n;i>0;i--)printf("%s",a[i]);printf("\n")}'
>> CBA 654 321 gfedcba
>> $ echo abcdefg 123 456 ABC | gawk
>> 'BEGIN{FS=""}{for(i=NF;i>0;i--)printf("%s",$(i));printf("\n")}'
>> CBA 654 321 gfedcba
>>
>> I would expect these concat versions to perform better, but I leave that
>> as an exercise for the student ;)
>>
>> $ echo abcdefg 123 456 ABC | gawk
>> 'BEGIN{FS=""}{x="";for(i=NF;i>0;i--)x=x $(i);print x}'
>> CBA 654 321 gfedcba
>>
>> $ echo abcdefg 123 456 ABC | gawk '{n=split($0,a,"");for(i=n;i>0;i--)x=x
>> a[i];print x}'
>> CBA 654 321 gfedcba
>>
>> in addition to the substr() example in your referenced post
>>
>>
>>
>> On Wed, Apr 24, 2019 at 10:57 PM 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
>


-- 
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]