Texas outline

c_progressbar

30 August 2010

I do a lot of programming, in a lot of different languages. Each language evokes a particular sort of mindset when I’m using it, as well: Python and Objective-C are for games, Ruby and C are for research, PHP is for webdev. Within each mindset, though, there’s a fair amount of blending — and I can’t tell you how often I wish I was writing in Ruby when I find myself writing in C.

My usual workflow for research-oriented coding involves prototyping a tool or model in Ruby (fast to write, easy to understand), then porting the final, working version into C for use with real data (my models are usually trained on massive statistical corpora, something Ruby is, unfortunately, ill-equipped to handle). This is all well and good, but I always miss the usability that I can so easily sprinkle into the Ruby version. OptionParser is one such gem; Ruby/ProgressBar is another.

In a moment of frustration, I re-implemented Ruby/ProgressBar in pure, unadulterated C99. If you’re interested in that sort of thing, you can grab a copy from my GitHub repository:

git clone git@github.com:doches/progressbar.git

For comparison, here is how you use a progressbar in Ruby:

require 'rubygems'
require 'progressbar'

progress = ProgressBar.new("Loading",100)
(0..99).each do |i| 
  # Do some stuff
  progress.inc
end
progress.finish

…and in C:

#include "progressbar.h"

progressbar *progress = progressbar_new("Loading",100);
for(int i=0;i<100;i++) {
  // Do some stuff
  progressbar_inc(progress);
}
progressbar_finish(progress);

More examples, including custom formatting and indeterminate progress, are in test/progressbar_demo.c.

« Previous: CogSci 2010