3 ms·
Yet the time cost of starting UNIX userland programs, including a crunched sh, busybox or simply a static dash, is perceptively less than starting Python. Ther
by 1vuio0pswjnm7 5y ago
Yet the time cost of starting UNIX userland programs, including a crunched sh, busybox or simply a static dash, is perceptively less than starting Python.
There is a lot of ridiculous criticism about shell scripts on the web in recent times. The author admits that it is unlikely a scripting language will outperform sort, sed and so on. However if I share something I wrote entirely in sed that can also be done in, say, Python with fewer LOC (because the work is being done by libraries) or in some other, large, popular scripting language, virtually no one commenting will appreciate the speed benefits of using sed.
I can deliberately avoid pipelines, I can store transformed data in temporary files, but most times I want to use pipes. They do not slow things down, they speed things up. That's because I do not write long, convulated shell scripts.
Many people want to write larger, more complex scripts. If so, then by all means use Python or whatever. But for smaller, simpler scripts, there is nothing wrong with the shell. Pipes are incredibly useful. People make shell scripts slow and then try to blame the shell.
Also, you forget to mention using {} which should not create a subshell.
- 1vuio0pswjnm7 5y agos/convulated/convoluted/
- Brian_K_White 5y agoI'm not sure what you're arguing. I was only explaining how busybox does not do what the previous commenter thought. The authors entire point about sort was to show just how little it's speed matters, because the job is rarely 90% within sort, or any other util like that (I don't count awk because awk is a fully functional language like sh, you could do the entire job in awk). How fast sort sorts 100 lines of text doesn't matter next to the cost of forking to execute sort, especially if it will happen repeatedly to do small jobs, treating it like a function. The reason it's bad is that there are so many and so small such things, and it adds up, and the shell is specifically designed to hide the cost of forking a process by making keywords and executables indistiguishable. And so you end up with a zillion forks just to get the current time or to strip a few bytes off of a few bytes long string, or even to do nothing but simply store a value in a variable (date, basename/dirname, foo=`generatefoo`) Sure, that doesn't mean don't use sh, nor does it mean do the contortions necessary to write in sh but avoid that problem, all that means is don't write non-trivial apps in sh. ... well unless you need the ubiquity more than the performance or readability. So it doesn't even necessarily mean that automatically. temp files - It would make no sense in most cases to go through pains to avoid forks only to use temp files instead. Creating and destroying a file, even in tmpfs(ram), is as bad or maybe even worse than a process. {} - I didn't forget {}, it had no special bearing on an explaination of what the author was talking about that makes shell scripts slow, and why busybox does not change that. It's just a branching structure like any other.