News for hackers. Stuff that matters.
Re:How is GNU `yes` so fast? by belorn on Tuesday June 13, 2017 @07:33AM (#14544046)
If you want to maximize readability and simplicity then writing `yes` in C is a bad choice. It is much easier, cleaner and shorter to write it in python or just use the shell which would normally be using the `yes` in the first place. Since `yes` is used in shells and builtins can be considered very reliable, here is a implementation as a shell function:

    yes(){ while :; do echo "${1:-y}"; done; }
Python:

    import sys 
    while True:
      if len(sys.argv) > 1:
        print(sys.argv[1])
      else:
        print("y")
And if you don't need the features of `yes` and only need a loop that prints 'y', then there really is hard to beat the simplicity of:

  while:; do echo "y"; done
Re:How is GNU `yes` so fast? by raptorfactor on Tuesday June 13, 2017 @07:42AM (#14544076)
Is it really "easier, cleaner and shorter to write it in python"? Did you look at the OpenBSD implementation?

https://github.com/openbsd/src/blob/master/usr.bin/yes/yes.c

It's essentially line for line identical to your python code...

Re:How is GNU `yes` so fast? by belorn on Tuesday June 13, 2017 @09:38AM (#14544599)
The C example has three includes, two conditionals, two loops, and one function definition. The python example has a single include, conditional, and loop.

For readability purpose it is easier to go through each lines of the python program than the OpenBSD C code. Its not massively different, but its distinguishable enough that I would choose the python version if I wanted to maximize readability, minimize syntax requirement and did not want to use shell script.

The Shell function is in my view the superior choice if the audience is a programmer than know the shell script syntax. It is just a single loop and is written in the environment that the program is intended to be used in. The only drawback there is the speed.

Re:How is GNU `yes` so fast? by mikeash on Tuesday June 13, 2017 @10:09AM (#14544787)
Most of what makes the C program bigger comes from the fact that the C program does more. Your python example doesn't call pledge(). Remove that from the C program and it drops to one include, one conditional, and two loops. Further, counting the two loops against C doesn't make any sense: it's entirely up to the programmer whether to have a conditional containing two loops, or a loop containing a conditional. Both languages could naturally do it either way.
Re:How is GNU `yes` so fast? by teamhappy on Tuesday June 13, 2017 @11:08AM (#14545314)
> The python example has a single include, conditional, and loop.

... and python. Don't forget to count python.

Re:How is GNU `yes` so fast? by BenjiWiebe on Wednesday June 14, 2017 @02:49PM (#14555009)
Exactly. That's the reason I write stuff in C instead of my favorite interpreted language, Ruby. When you write something in C, that's it. No large interpreter plus runtime needed.
[dead]
Re:How is GNU `yes` so fast? by sctb on Tuesday June 13, 2017 @01:27PM (#14546615)
You've been on a tear of uncivil and unsubstantive commenting, and it has to stop. Often a good strategy for this is to slow down. High-quality reflective posts are what we're after instead of dismissive, snarky reflexive ones, and the former come more slowly.
Re:How is GNU `yes` so fast? by _RPM on Tuesday June 13, 2017 @09:36AM (#14544591)
This is innificient. Why do the argv check in every iteration of the while loop? It's not going to change between iterations.