Friday, June 24, 2011

How Fast Can You Type? Develop a Tiny Utility in Bash to Find Out

SkyHi @ Friday, June 24, 2011
If you spend most of your time typing on your keyboard (and I hope you don’t use that mouse very frequently, if you care for your wrists, that is), getting up to speed and practicing to become a better and faster typist is well worth the time and effort. And measuring something is the first step to improve it.
There are tons of applications which test your typing abilities and help you improve it, but wouldn’t it be nice to have a basic idea about your typing performance using nothing but good old Bash? After all, this is about DIY (Do It Yourself) approach and having fun; two notions that Linux Journal readers know very well.
The idea is actually very simple: Measuring the typing speed means basically measuring how many words you typed in a given amount of time. One of the most popular units used is wpm (words per minute). Maybe not very accurate and scientific, but we’re aiming for a ballpark figure here so some approximate measure will be fine for our purpose. Based on this information we can write the formula as:
typing_speed_in_wpm = num_words / ( (end_time - start_time) / 60 )
Now that we have our theoretical framework set up, it is time to build the practical computational part of the project. Before diving into code, let’s break down the above formula into pieces and see which GNU/Linux utilities can help us achieve various tasks:
  • date: This is our well-known utility and if you use it with the %s format specifier it returns the “seconds since 1970-01-01 00:00:00 UTC”. So if you run date +%s once at the beginning of your typing session and once at the end of it, you’ll have the end_time and start_time.
  • wc: This is yet another well-known utility that can give you the number of words in a file if invoked with -w option. And remember, in GNU/Linux almost everything is a file, including your input from the keyboard.
  • cat: Officially it concatenates files and print them on the standard output. Practically it can grab your input from the keyboard and via a pipe send that to the wc. In other words all we need to do to count the number of words we just typed is to issue the following command: cat | wc -w
  • bc: Officially it is an arbitrary precision calculator language. Practically it is a very handy utility if you want to do calculations within the command line. But you have to be careful and read its manual page. Why? Well, if you try these:
$ echo 1 + 1 | bc
everything seems fine but if you try the following:
$ echo 1 / 2 | bc
That’s not what you’d expect from a computer. Why doesn’t it return the correct answer, that is 0.5? According to its manual page “scale defines how some operations use digits after the decimal point. The default value of scale is 0.” Apparently it is not a very sensible default for our division operations which we’ll use later. The solution then is to tell bc what scale to use before doing the operation, e.g.:
$ echo “scale=2; 1 / 2” | bc
That’s much better. We have all the components in place and now it is time to glue them together using our favorite application development environment, Bash:

# a very tiny utility to measure typing speed.
prompt="Start typing a piece of text. Press Ctrl-d twice to finish."
echo "\n$prompt \n"
start_time=`date +%s`
words=`cat|wc -w`
end_time=`date +%s`
speed=`echo "scale=2; $words / ( ( $end_time - $start_time ) / 60  )" | bc`
echo "\n\nYou have a typing speed of $speed words per minute."

If you save the above shell script as and make it executable, you are ready to measure your typing speed. Oh, I forgot one thing, that is a piece of text to type. It is always good to have some text ready so that you’ll know what you type. In this case I prefer the first few lines of the Usenet message of Linus Torvalds in which he announced the birth of Linux:
$ ./
Start typing a piece of text. Press Ctrl-d twice to finish.
Hello everybody out there using minix -

I'm doing a (free) operating system (just a hobby, won't be big and
professional like gnu) for 386(486) AT clones. This has been brewing
since april, and is starting to get ready.

You have a typing speed of 43.33 words per minute.
Well, 43.33 words per minute is not a world record for sure (and I definitely had better scores, believe me!). According to the relevant Wikipedia article, “as of 2005, writer Barbara Blackburn was the fastest English language typist in the world, according to The Guiness Book of World Records. Using the Dvorak Simplified Keyboard, she has maintained 150 words per minute (wpm) for 50 minutes, and 170 wpm for shorter periods. She has been clocked at a peak speed of 212 wpm.” I don’t know, maybe it is time to switch to the Dvorak simplified keyboard but I have my doubts.
The tiny utility above helped me to have a rough idea about my typing speed. Certainly it lacks some important features. It would be nice if:
  • It included various texts and showed them in a random order so that the average performance of different typing sessions can be calculated. A single measurement is hardly a reliable indicator when it comes to this kind of benchmarking.
  • It had the option of getting sample texts from files.
  • It took into account the number of errors made by the typist. This calls for a small function that can compare the sample text and the input of the typist. It does not mean much if your performance is 1000 wpm but 90% of it includes terrible errors, typos, etc.
The three points above are left as an exercise to the Linux Journal reader.
Happy hacking and typing.