Re: [hackers] [sbase][PATCH] Minor optimizations for 'yes'

From: Michael Forney <mforney_AT_mforney.org>
Date: Wed, 26 Jun 2019 01:04:50 -0700

What's the motivation for this? Are you running into performance
issues with yes(1), or is this just for fun to increase the throughput
for some benchmark?

I'd be in favor of changing yes if it increases readability (while
terse, the existing logic is a bit hard to follow), but I don't think
this patch does that.

On 2019-06-25, AGitBoy <aidanwillie0317_AT_protonmail.com> wrote:
> ---
> yes.c | 17 ++++++++++++-----
> 1 file changed, 12 insertions(+), 5 deletions(-)
>
> diff --git a/yes.c b/yes.c
> index dd97ea6..49f5ed6 100644
> --- a/yes.c
> +++ b/yes.c
> _AT_@ -6,13 +6,20 @@
> int
> main(int argc, char *argv[])
> {
> - char **p;
> + if (argc > 1) {
> + char **p;
>
> - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;
> + argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;
>
> - for (p = argv; ; p = (*p && *(p + 1)) ? p + 1 : argv) {
> - fputs(*p ? *p : "y", stdout);
> - putchar((!*p || !*(p + 1)) ? '\n' : ' ');
> + for (p = argv; ; p = (*p && *(p + 1)) ? p + 1 : argv) {
> + fputs(*p, stdout);
> + putchar((!*p || !*(p + 1)) ? '\n' : ' ');
> + }

In this case, since we know there is at least one argument, *p is
always true, and I think some simplifications fall out of that.

> + } else {
> + while (1) {
> + fputc('y', stdout);
> + fputc('\n', stdout);
> + }

I think I'd prefer

        for (;;)
                puts("y");

here.

> }
>
> return 1; /* not reached */
> --
> 2.21.0
Received on Wed Jun 26 2019 - 10:04:50 CEST

This archive was generated by hypermail 2.3.0 : Wed Jun 26 2019 - 10:12:24 CEST