Xaprb

Stay curious!

Archive for May, 2009

A productivity tip for test-driven development

with 3 comments

If you code by writing tests that fail, and then fixing the tests by writing the code, then you might find yourself switching to a terminal, running the test, ad nauseum. Part 1 of my tip is to run the test in a loop that takes a single keystroke to trigger:

$ while read line; do clear; perl MyTestScript.t; done

This works with any language, not just perl — just replace the test command with the right one. ALT-TAB, press Enter, ALT-TAB back to your editor.

Part 2 of my tip is to make it really easy to drop into the debugger if you want. Notice the small change here:

$ while read line; do clear; perl $line MyTestScript.t; done

Now instead of pressing Enter, you can type “-d” and press Enter. Presto, you’re in the debugger. This also works for any language that has a built-in debugger. Of course, you can also pass any other arguments you want, such as enabling profiling.

Written by Xaprb

May 3rd, 2009 at 5:35 pm

Posted in Coding,Perl

Tagged with

An easy way to run many tasks in parallel

with 12 comments

Domas Mituzas mentioned this recently. It’s so cool I just have to write about it. Here’s an easy command to fork off a bunch of jobs in parallel: xargs.

seq 10 20 | xargs -n 1 -P 5 sleep

This will send a sequence of numbers to xargs, which will divide it into chunks of one argument at a time and fork off 5 parallel processes to execute each. You can see it in action:

$ ps -eaf | grep sleep
baron     5830  5482  0 11:12 pts/2    00:00:00 xargs -n 1 -P 5 sleep
baron     5831  5830  0 11:12 pts/2    00:00:00 sleep 10
baron     5832  5830  0 11:12 pts/2    00:00:00 sleep 11
baron     5833  5830  0 11:12 pts/2    00:00:00 sleep 12
baron     5834  5830  0 11:12 pts/2    00:00:00 sleep 13
baron     5835  5830  0 11:12 pts/2    00:00:00 sleep 14

There are basically unlimited uses for this!

Written by Xaprb

May 1st, 2009 at 11:17 am