top of page

[Programming] 4 steps you make your script efficient!




A few days ago, I talked about improving efficiency through programming.


More specifically, programming itself can also be made more efficient.


The procedure is:

  1. [Human, once] Replace the visual manipulation with a text-based one.

  2. [Machine, once] Replace the text-based instructions with something more efficient.

  3. [Machine, multiple times] Make the computer do the same thing automatically dozens or hundreds of times.

  4. [Human, multiple times] Simplify instructions that are done multiple times.


These four processes can be divided into two dimensions: "human or machine efficiency" and "one-time or multiple-time efficiency".


1) [Human, one time] Replacing visual manipulation with text-based.


Replacing a human task with a computer is a great way to improve efficiency. This is also similar to what I wrote the other day about YouTube and the efficiency of books.


Visual manipulation with a mouse is easy to understand, but it is not explicit and can lead to errors. For example, when cropping an image, if you use the mouse cursor to crop the image, there is a possibility that the image will be cut by 49% when it should be cut by 50%. By writing a script that tells the computer to crop at 50%, you can eliminate this error and speed up the process.


For example, on a Macbook, if you have ImageMagick and Poppler installed, you can crop an image vertically at 50% and create two images, as shown below. By the way, if you reverse the 50% and 100% in the script below, it will cut the image horizontally. +repage will add "-0" and "-1" to the end of the book.jpeg that is cropped at 50%.



convert -crop 50%x100% +repage book.jpeg book.jpeg



2) [machine, once] Replace the text-based instructions with something more efficient.


It's the same job, but it seems to work a bit faster if you use the 'tile' function specified below. It may not seem like you're working on the efficient side initially, but the next step will make a difference after hundreds or tens of thousands of operations. If you ask, 1)

to 2), if you save 0.1 seconds, you will save 10 minutes when processing 6000 files.



convert -crop 2x1@ +repage book.jpeg book.jpeg



3) [Machine, multiple times] Make the computer do the same thing automatically dozens or hundreds of times.


Then you can make the computer do the same simple task tens of thousands of times, infinitely more efficient. Let the computer do the replaced simple task multiple times.


This is what happens when you digitise a paper-based report book that you have stored and cut it in half. If you have a report with hundreds of scanned facing pages, it would be difficult to hit the script above each time.


If you run the script in a 'for' loop like this, it will split all the JPEG files in the directory in half vertically. '$fname' is the name of the JPEG file in this directory.



for fname in *.jpeg; do
convert -crop 2x1@ +repage $fname $fname ;
done


4) [human, multiple times] Simplify instructions that are done multiple times.


Finally, convert this 'for' loop into a script that is executed in one line, called a one-liner.



ls -1 *.jpeg | sed 's/. */& &/' | xargs -n 2 convert -crop 2x1@ +repage


This one-liner is written on a single line, so it's easy to run and edit. Not only is it more efficient for commanding and editing by a human. Depending on the type of programming, it can also improve processing speed. In statistical programming R, this kind of one-liner makes the programming process not only visually but actually more efficient.


The vertical bar '|' is called piping, and the output from the command before the vertical bar is passed as input after the vertical bar. The first command extracts all the JPEG filenames, and the second command repeats the filenames. The repeated command is then passed to the final command, which crops the image file with the names of the 'JPEG to be processed' and 'processed JPEG file', respectively. 'xargs' supports the passing of multiple such inputs. Be careful with this one-liner, as it may not work if you have a filename containing spaces.


Programming is fun, and it helps you to create leisure time, so you should learn it ;-)

70 views0 comments

Recent Posts

See All

Opmerkingen


bottom of page