Saturday, October 9, 2010

Installing Rails 3 in Ubuntu 10.4

For the record, and for those of my friends who are going to participate in the Rails Rumble next weekend: here's how you install Rails 3 on your Ubuntu Karmic system. (10.10, known as Maverick, is supposed to arrive tomorrow and might make this post obsolete. But I won't be risking an upgrade before the weekend.)

  1. Install Ruby; apt contains the required version:
    sudo apt-get install ruby
  2. Uninstall RubyGems, because we'll install a more recent version from source:
    sudo apt-get remove rubygems
  3. Install RubyGems from source:
    cd /tmp
    tar xzf rubygems-1.3.7.tgz
    cd rubygems-1.3.7
    sudo ruby setup.rb
    sudo ln -s gem1.8 /usr/bin/gem
  4. Install Rails:
    sudo gem install rails

Sunday, July 18, 2010

Exporting slices from Inkscape

If you have a Python interpreter available, you can use the slightly better alternative from Part 2.

In Photoshop, it's possible to “slice” an image into pieces, name the individual pieces, and export each to a separate file. This is really useful for web design: you can do the whole design in one image, then export the bits and pieces for later reassembling in HTML/CSS. Can we do the same slicing trick with Inkscape?

It turns out that we can; it's even on the Inkscape Tips and Tricks page. However, the exporting itself remains manual labour. I wrote a bash script to automate that; the code is given below.

The process is as follows:

  • First, draw your image as usual.
  • Then, add a layer that will hold the slices; name it, for example, slices. Set the layer's opacity to about 50% to be able to see what you're doing. Enable the grid, and make sure it is set to pixels: you want your slices to align with pixel boundaries.
  • Draw your slice rectangles onto the slices layer, aligned to the grid. Ensure that the rectangles have no border; the fill is irrelevant (I use red).
  • Right-click a slice, and choose Object Properties. Change Id field to EXP-something and hit Enter. The tag EXP- indicates that this rectangle is intended for export; something will become the filename (something.png). Repeat this for all slices.
  • Hide the slices layer, and save your image. Let's say you called it layout.svg.
  • Run the script as follows:
    ./ layout.svg
    You should see each of your slices being exported to a PNG file in the same directory.

Here's the code for the script. Save this to a file and make it executable.


if [[ -z $1 ]] ; then
 echo "Usage: $0 [FILE]"
 exit 0


for ID in `grep -o "id=\"$PREFIX.*\"" $FILENAME | cut -d\" -f2` ; do
 echo "Exporting area $ID to $OUTPUT..."
 inkscape --export-id=$ID --export-png=$OUTPUT --file=$FILENAME

Thursday, May 20, 2010

Are C++ iostreams really slow?

In the C++ world, one often hears the statement “iostreams are slow, you should use printf instead”. Is this true?

It is possible that this story results from abuse of endl. Beginner's C++ books often recommend endl over '\n' for writing a newline. The observable result is often the same, but endl results in extra overhead: it flushes the stream. This results in a system call, slowing things down while often unnecessary.

Using my newly written TimedSection class, I tested whether the story is actually true, or just a myth. I tried three methods of printing:

  • cout with endl for line endings
  • cout with '\n' for line endings
  • printf (also with '\n' for line endings, so no forced flushing)

Each of these was tested in the following scenarios:

  • printing an empty line
  • printing a line containing a string literal
  • printing a line containing a string variable, a string literal, and an integer variable

All strings used were 20 characters long; I figured this would be a typical average length for printing messages. Newline characters were absorbed into string constants wherever possible.

Each of these was run 10 million times, redirecting the output to /dev/null. The testing machine is an Intel i7 920, running Ubuntu 10.4, Linux 2.6.32 and gcc 4.4.3.

Here are the results:

cout with only endl                     1461.310252 ms
cout with only '\n'                      343.080217 ms
printf with only '\n'                     90.295948 ms
cout with string constant and endl      1892.975381 ms
cout with string constant and '\n'       416.123446 ms
printf with string constant and '\n'     472.073070 ms
cout with some stuff and endl           3496.489748 ms
cout with some stuff and '\n'           2638.272046 ms
printf with some stuff and '\n'         2520.318314 ms

Surprise! Yes, for printing newlines, printf greatly outperforms cout. We can also see the huge slowdown of using endl inappropriately, something which is clearly to be avoided.

However, even for printing a moderately-sized string constant, cout outperforms printf. This effect becomes more pronounced as the string gets longer, probably because printf has to do more processing per character.

Finally, for some slightly more complicated formatting, the difference is quite small. For longer string constants, it becomes even smaller.

We can conclude that iostreams are definitely not always slower than C-style printf. It depends on the particular use, and as the formatting becomes more complicated, the difference drops to zero. Part of the myth may be ascribed to inappropriate use of endl; maybe another part is due to unoptimized implementations of the standard library. Either way: myth busted.

Update: For the curious, here is the full source code. You may need to link with -lrt to get clock_gettime.

#include <stdio.h>
#include <iostream>
#include <ctime>

class TimedSection {
    char const *d_name;
    timespec d_start;
        TimedSection(char const *name) :
            clock_gettime(CLOCK_REALTIME, &d_start);
        ~TimedSection() {
            timespec end;
            clock_gettime(CLOCK_REALTIME, &end);
            double duration = 1e3 * (end.tv_sec - d_start.tv_sec) +
                              1e-6 * (end.tv_nsec - d_start.tv_nsec);
            std::cerr << d_name << '\t' << std::fixed << duration << " ms\n"; 

int main() {
    const int iters = 10000000;
    char const *text = "01234567890123456789";
        TimedSection s("cout with only endl");
        for (int i = 0; i < iters; ++i)
            std::cout << std::endl;
        TimedSection s("cout with only '\\n'");
        for (int i = 0; i < iters; ++i)
            std::cout << '\n';
        TimedSection s("printf with only '\\n'");
        for (int i = 0; i < iters; ++i)
        TimedSection s("cout with string constant and endl");
        for (int i = 0; i < iters; ++i)
            std::cout << "01234567890123456789" << std::endl;
        TimedSection s("cout with string constant and '\\n'");
        for (int i = 0; i < iters; ++i)
            std::cout << "01234567890123456789\n";
        TimedSection s("printf with string constant and '\\n'");
        for (int i = 0; i < iters; ++i)
        TimedSection s("cout with some stuff and endl");
        for (int i = 0; i < iters; ++i)
            std::cout << text << "01234567890123456789" << i << std::endl;
        TimedSection s("cout with some stuff and '\\n'");
        for (int i = 0; i < iters; ++i)
            std::cout << text << "01234567890123456789" << i << '\n';
        TimedSection s("printf with some stuff and '\\n'");
        for (int i = 0; i < iters; ++i)
            printf("%s01234567890123456789%i\n", text, i);

Timing a section of code in C++

Sometimes it's useful to measure how long a certain part of your C++ code takes to execute. Here's a little utility class that makes this extremely easy. It can be used like this:

void foo() {
    TimedSection s("foo()");
    // do some long operation...

Calling foo() will print something like this to stderr:

foo()   336.856127 ms

As you may have guessed, a TimedSection records the time between its construction and its destruction. Since C++ has no garbage collection, destructors are called at a deterministic moment: when the local object (called s above) goes out of scope. If you want to time a section that is not a scope by itself, you can introduce a pair of braces to scope the object. If the section is even more complex (e.g. spanning multiple functions), you can use new and delete, but that destroys the elegant simplicity of TimedSection's usage.

The implementation is rather simple as well:

#include <iostream>
#include <ctime>

class TimedSection {
    char const *d_name;
    timespec d_start;
        TimedSection(char const *name) :
            clock_gettime(CLOCK_REALTIME, &d_start);
        ~TimedSection() {
            timespec end;
            clock_gettime(CLOCK_REALTIME, &end);
            double duration = 1e3 * (end.tv_sec - d_start.tv_sec) +
                              1e-6 * (end.tv_nsec - d_start.tv_nsec);
            std::cerr << d_name << '\t' << std::fixed << duration << " ms\n"; 
I hereby release this little code snippet into the public domain. Enjoy!

Monday, April 5, 2010

So long, and thanks for all the glass

When I first installed Windows 7 after nearly a decade of using XP, I decided I'd not customize it. I'd just leave the settings at their default values and see what the wise men at Microsoft had decided for me. After all, there are thousands of hours of usability testing behind those decisions, so it seemed a good idea to give them a chance.

Now, half a year later, I've gotten used to most of Windows 7. Although I've tweaked the occasional thing here and there (show me the damn file extensions already!), I think most of it is pretty reasonable. But there's one thing I just can't get used to: the Aero theme.

It's not that it's semi-transparent. It's not that it fades and zooms and zips and zaps. It's just the minor little thing that I can't see the difference anymore between active and inactive windows.

When you activate a window, the title bar and borders become a little bit darker, the shadow becomes a little bit blacker, and the close button changes its colour from glassy to red. You can see the changes happen while you're switching focus, but telling the difference at a glance from a static image is hard. And because the title bar is transparent, the colour depends on the things behind it, making it even harder. There's just no single, consistent look for active and inactive title bars. I kept having to look at the close button to tell which window was active.

The subtle difference between an inactive and active title bar in Windows Aero

So what am I to do? Am I to magically know which window is active? When I've just clicked it, or when I'm typing into it, sure, I'll know. But being the Alt+Tab guy that I am, I shift focus without clicking all the time, and not always to the correct window right away. And what if some unexpected window suddenly pops up that might have stolen my focus, like an auto-updater? How can I tell if I can continue typing, or that I'd better yank my hands away from the keyboard in case I inadvertedly activate the Launch Nukes button?

Unfortunately, there is no way to change the look. The wise men at Microsoft have decided that a “theme” will be nothing more than a single colour for the glass, along with a set of wallpapers. There's no way to select different colours for active and inactive windows, like there is for the “classic” themes. There's also no way to install a clearer Aero theme without resorting to commercial third-party software like WindowBlinds.

So in the end, after my millionth focus fuckup, I switched back to the classic Windows 2000 lookalike theme. Blue versus gray seems like a distinction my eyes can handle.

Why do I have to sacrifice good looks to gain usability? Why can't Microsoft just make them get along?

Saturday, March 20, 2010

A harmonograph in JavaScript

What's a harmonograph, you ask? Well, it's something like a Spirograph for grown-ups. A harmonograph uses a construction with pendulums to draw pretty patterns.

One particular incarnation uses two pendulums of nearly equal length, each about two metres. To the right pendulum, a slowly revolving disk is attached, which is driven by a small electrical motor. The drawing paper is taped onto the disk. To the left pendulum, an arm is attached with the pen at the end. When the pressure and thus the friction of the pen is sufficiently low, the pendulums can keep swinging for tens of minutes.

For learning and fun, I replicated this harmonograph in JavaScript with the HTML5 canvas element. The pendulum motion is approximated by a sine function, and friction by an exponential, but apart from that it should be pretty physically accurate.

Without further ado, I present to you: Harmonograph in JavaScript. Usage should be self-evident. Although some input validation is done, you will be able to break it with strange values.

With the link at the top, you can save and bookmark your creations or send them to your friends. Enjoy!

Update, 2017-01-22: Hosting is now done here on GitHub Pages. The old location redirects you. I also published the source code (“View Source” still works just fine, but it's now officially MIT licensed as well).

Sunday, March 14, 2010

Capitalizing MP3s from the command line

For my own future reference and your enjoyment, here's a neat little oneliner to transform 01 - some song.mp3 into 01 - Some Song.mp3:

rename "s/([^._'])\b(\w)(\w*)/\$1\U\$2\L\$3/g" *.mp3

It needs the prename utility from Perl, which on Ubuntu is available by default under the name rename.

Friday, February 5, 2010

Ubuntu fails to load nvidia kernel module

As much a note to myself, as a warning to others…

After a kernel upgrade, my Ubuntu Karmic started the X server in low-resolution mode. My Xorg.0.log said:

(II) LoadModule: "nvidia"
(II) Loading /usr/lib/xorg/modules/drivers//
(II) Module nvidia: vendor="NVIDIA Corporation"
        compiled for 4.0.2, module version = 1.0.0
        Module class: X.Org Video Driver
(EE) NVIDIA: Failed to load the NVIDIA kernel module. Please check your
(EE) NVIDIA:     system's kernel log for additional error messages.
(II) UnloadModule: "nvidia"
(II) Unloading /usr/lib/xorg/modules/drivers//
(EE) Failed to load module "nvidia" (module-specific error, 0)
(EE) No drivers available.

I tried modprobe nvidia and such, but it seemed that the module actually did not exist. This module should be installed by the package nvidia-185-kernel-source, which was present on my system. However, it turns out that the kernel module is compiled on-the-fly by a program called jockey which controls DKMS, the Dynamic Kernel Module Support.

It is possible to force a recompile using dpkg-reconfigure:

$ sudo dpkg-reconfigure nvidia-185-kernel-source
Removing all DKMS Modules
Loading new nvidia-185.18.36 DKMS files...
Building for architecture x86_64
Module build for the currently running kernel was skipped since the
kernel source for this kernel does not seem to be installed.

I need the kernel source, eh? Why the hell is that not a dependency, if the driver package is useless without it? Anyway, let's install the kernel source then:

$ uname -r
$ sudo apt-get install linux-source-2.6.31

Installs fine, but makes no difference. Turns out that dpkg-reconfigure was lying: I just need the headers. Here we go:

$ sudo apt-get install linux-headers-2.6.31-19-generic
$ sudo dpkg-reconfigure nvidia-185-kernel-source
Removing all DKMS Modules
Loading new nvidia-185.18.36 DKMS files...
Building for architecture x86_64
Building initial module for 2.6.31-19-generic

Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/2.6.31-19-generic/updates/dkms/


DKMS: install Completed.
$ modprobe nvidia

That's better.

Several bug reports indicate similar problems, but the current way this is handled is terribly inadequate. The driver package should pull in the kernel headers if it needs them. There was no warning about this when the kernel was upgraded. There was no warning when the module failed to compile on boot. A fix for a problem with the same symptoms was released back in December; another one is in the upcoming Lucid release.

Oh yeah, I ended up rebooting my system. Whatever happened to Ctrl+Alt+Backspace? (Answer.)

Update, 2010-08-04: After another kernel upgrade, my display driver was hosed again. After hours of tinkering, I typed sudo dpkg-reconfigure nvidia-current and was greeted with the message gzip: stdout: No space left on device. Apparently, my /boot partition was full (of abandoned kernels). Something to check, for whoever runs into similar problems! Also, the kernel module appears to have been renamed from nvidia to nvidia-current.

Sunday, January 24, 2010

Review of free C++ IDEs for Linux

For my new project, I started looking into IDEs for Linux for the C++ language. And since I'm testing them anyway, I might as well write some quick reviews. ‘Quick’, because I'll mostly be going on first impressions, and some things I say might be quite wrong. Feel free to correct me in the comments!

My previous IDE experience includes Visual Studio for C++ and for C#, Eclipse for Java. I will sometimes compare to these.


I am greeted with very nice wizards, and it's easy to get up to speed. There is even a template for GLFW projects, which I select. But when trying to compile (using shortcuts nothing like any other IDE I know) I get a linker error; some XF86 library is missing, and even apt-file cannot tell me where to get it.

Hunting through the build settings, I notice many other problems. The settings are nested three levels deep: a bar with huge icons on the left, scrolling tabs (the horror) on the right, and nested tabs below those. Many options are unclear. When I press Esc to cancel my changes and dismiss the dialog, nothing happens.

Some parts of the Code::Blocks UI are really good, other parts are… not so good. I wouldn't want to work in this for a long time.


It starts up with a blank screen, so I have to ask it to create a new project myself. Not a big deal. The New Project wizard is reminiscent of Visual Studio, so I feel quickly at home. It allows me to create a CMake project; I like CMake. The wizard gives me a Hello World program, but I cannot run it: I have to create a Launch Configuration first. Then I run my program, I think, but where does the output go? The Debug Area (‘Perspective’) is empty. Turns out that building failed, because I moved the project in the mean time, and the project file contains absolute paths. Bad!

Support for external tools seems good. I already mentioned CMake; the Valgrind profiler and Git versioning system can also be used right from the IDE.

The IDE itself actually seems like a blend of Eclipse (borrowing much of its concepts and terminology), Visual Studio (some wizards and the layout of dialogs) and Kate (the general KDE stuff). Not bad at all. Still, I had some trouble getting everything set up, but once you've taken the time to do that, KDevelop might be a decent IDE.


Big, bloated, but feature-rich and quite user-friendly once you've warmed up to it. That's been my impression of Eclipse for Java, which I'm quite familiar with. For Java, it's easily the best IDE around; the refactoring options alone make it worthwhile. Let's see how Eclipse with the CDT plugin performs for C++.

I install Eclipse, grab the CDT plugin through its update site and restart Eclipse. A few clicks later, I have an empty C++ project. I add a main Hello World file, and after some strange trouble with the Debug Configuration, it runs. I add a library to link with, finding the option without any difficulty. Not being unhappy about this, I continue to write some more code, to try the completion and refactoring features.

Completion pleasantly surprises me. This stuff is better than Visual Studio's. It's fast, it's accurate, it gets the contents of a newly included header file right after I saved… nice! Smart insert mode adds closing paretheses, braces and quotes, but if I type them myself as well, they are overwritten.

Error highlighting is equally fancy. Unlike Visual Studio, syntax errors are highlighted in real time, without even having to save the file.

I ask it to create a new class, and get a header and source file with a nice skeleton. It's not exactly the way I would write it, but I'm sure it's customizable.

Refactoring, unfortunately, is not as fancy as it is for Java. It is limited to renaming identifiers, and doesn't rename the source and header file accordingly. It can also extract selected expressions/statements into a separate function, but it does not create this as a member function. There are no “quick fix” options for adding include directives, like with Java. Yet, it's better than what Visual Studio offers on this front.

Eclipse with the CDT plugin is not perfect, but it's good. I could live with it.


I am greeted with a welcome screen, which I ask to create a new project. I am asked for all kinds of needless details, like the license type and my e-mail address. No wonder: it creates an entire autotools project for me. I tried to add a library to the linker command, but couldn't find the option anywhere. Do I really have to edit the

I've only had a brief look, and Anjuta seems like a nice IDE if you want what it wants, but at first sight doesn't seem flexible enough to cope with everything.

Qt Creator

Would this work for non-Qt applications as well? It wouldn't seem so at a first glance, but I imported an empty directory as a Makefile project, which seemed to work. I had to write a Makefile, of course, which failed because the IDE quietly changed my tab to four spaces. After fixing that, and some directory issues, I could build the program. Running it required similar trickery.

Code completion works very well, and identifiers from a newly included header are available quickly. The entire IDE looks and feels smooth and polished, and is reminiscent of Visual Studio in many details.

The build system remains weird, though. It created an autotools project for me, with about a dozen files, and not offering me any choice in the matter. There is a reference to CMake in the Preferences, but no clue how to create a CMake project.

Qt Creator is a slick, clean, usable IDE. I like it. Still, it is very Qt-centric, and its build system seems inflexible. Too bad.


Yes, another Java IDE that can be used for C++. I have actually never used NetBeans for Java before, so I have no preconceptions about how it should work.

I apt-get the IDE and install the C/C++ plugin; this is very straightforward. I create a new C++ project, and a Makefile gets created automatically. I run the empty main function, and the program pops up in an external terminal.

Then I start editing the code, and I'm pleasantly surprised by the editor. Not just syntax errors, but unknown identifiers get highlighted in real time. Function signatures pop up in a large, but helpful tooltip. Even the filenames of include files are autocompleted! The contents of the include file become available without even having to save the file.

Yet, not all compile errors are highlighted, even though you can click to them from the compiler output window. I could not manage to get the function signature to reappear while I was typing arguments. Refactoring options are all grayed out; I assume those work for Java only.

The project settings are very much like Visual Studio, but allow for little customization. It's just enough to add a -l flag to the linker command line to link in an external library. There is also Git integration (if you install the plugin) but I didn't try it.

I checked out NetBeans as an afterthought, but that seems to have been a mistake. Of all the IDEs I tried, this is actually one of the nicest. It doesn't have many bells and whistles, and its build system might be inflexible, but the code editor makes up for it.


Are IDEs for C++ worth the trouble? I'm not sure. The value of an IDE lies in fast code editing, refactoring options, easy building, and error highlighting, and integrated debugging. None of the IDEs I tried score highly on all these fronts. I can understand: a flexible, usable IDE for C++ is almost impossible to create. Maybe I'll just go back to my trusted old Kate and gnome-terminal…

Tuesday, January 12, 2010

Programmers are not artists

As you may know, it takes a long time to install Visual Studio. Even on a modern system like mine, it took around twenty minutes. No program I know has ever gotten close to that, except Microsoft Office.

This means that the guy below is on your screen for a long time. Seriously, the longer you look at it, the more disturbing his face becomes.

In the style of Photoshop Disasters, I'm supposed to write a witty comment below the photo. How about this:

Management: “Hey, this guy is not smiling! Do something about it!”
Intern: “Whatever, I'll just photograph my girlfriend's mouth from this entirely different angle and slap it on. It's not as if this photo will be visible for a long time, anyway.”