[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] Efficient way to reverse a string
From: |
Andrew J. Schorr |
Subject: |
Re: [bug-gawk] Efficient way to reverse a string |
Date: |
Thu, 25 Apr 2019 22:42:44 -0400 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
On Thu, Apr 25, 2019 at 08:40:09AM -0500, Peng Yu wrote:
> I don’t need to print the reversed string but get the reversed string in
> memory.
If this is really so performance-sensitive, you can create an extension
C library function to implement this. And while you're at it, you may as
well implement a general-purpose `slice' function that works on arrays
and strings as Python's slice operator does.
By the way, in Python, it's something like this (to reverse on a line-by-line
basis):
import sys
for l in sys.stdin:
print(l[-2::-1])
It seems to be almost 10 times faster than the gawk solution using
substr to assemble the line (which is almost twice as fast as using
printf on each char separately).
FYI, the util-linux rev.c source code basically loads each line of the
file, then finds the end of line, and loops from the end of the line
to the beginning of the line calling putwchar on each character.
Regards,
Andy
- Re: [bug-gawk] Efficient way to reverse a string, (continued)
- 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 <=
- Re: [bug-gawk] Efficient way to reverse a string, arnold, 2019/04/28