Archive for the ·

English

· Category...

berp is awesome

no comments

berp is awesome. Run the plain boring implementation of factorial below with it to see how amazingly fast berp is (compare it with CPython.)

def fact(n):
    if n <= 1: return 1
    return n * fact(n - 1)
fact(90000)

Something worth trying

no comments

As occurs with many people with ADHD, I have troubles falling asleep (actually, the causation is reversed here), and an another problem getting up. Psyblog has article on how to train yourself, to associate your bedroom with sleep. Pavlov style.

Yep, that’s something worth trying.

If you don’t like it, go somewhere else

no comments

Tribute to Feynman.

Shit my professor says: green software

no comments

I’ve listen enough about green software – or any kind of low-power-consumption software for instance. One says programmers should

carefully look at the object code to minimize transistor state transition – where all the power consumption occurs.

I think this is a bunch of bullshit for two reasons. But first, allow to retort a bit. Low-power-consumption software is applicable in any device that is running on a battery (e.g. your phone), but the concept that’s being taught is completely overstated.

My two reasons:

  • processors like the ARM Cortex A8 on your iPhone 4 are superscalar. Which mean that they reorder the instructions in execution time to accelerate the overall program execution. Which makes the argument invalid. Looking at your object code will only be a waste of time.
  • there are better ways to reduce power consumption on devices. One of them is reducing the power dissipation in the transistor state transition. (Or maybe using a black-hole as a cooling device :) .)

You can argue that it may apply to smaller devices with simpler architectures. Surely I agree, but still who looks at object code these days? Let the compiler do hard work. And how many of the students will really work on these kind of computers? Not many.

Also, in phones the major power consumption doesn’t happen in the processor. The radios (GSM, 3G, GPS, wifi and Bluetooth) and the screen are the greatest wasters. Why focus on bits, when the phone screen alone wastes more than 90% of the phone’s battery? Minimize the time that screen and radios are on, and you will be saving many more watts (and time) than the power saved by perfectly aligned bits.

Another professor says that we should focus in reducing disc spins. There are no discs spins since the invention of flash drives. QED.

The same professor says that reducing the algorithmic complexity of the program will reduce the power consumption. With this I agree. It seems the single improvement in software engineering that will have the largest impact in devices’ power consumption. Reducing an O(n) algorithm to O(log(n)) will reduce the power consumption by one order of magnitude. That’s way more saving than the OCD behavior of aligning bits.

TLDR: carefully looking at bits to reduce power consumption will only work if the processor is not superscalar. Reducing the power consumption on transistors, screens and radios, and reducing the algorithmic complexity of your software are a better way of controlling power consumption, but don’t OCD  yourself over this.

If you have arguments against my case, the comment box is at your service :)

Testing Android

1 comment

I’m assuming that you already know how to create automated tests for Android applications. If you don’t you can read something in the Android Testing documentation.

I was trying to run some Android tests in an application that has several activities running in separate processes. If you have ever tried to do such thing, you know that it isn’t very straightfoward. It complains that it can’t instrument an activity whose intent was resolved in another process. Bummer!

I’ve spent some time thinking how to test this, and I figured a way. The activities that where put into testing didn’t need to run in separate processes when running alone, so simply extending the target activity and declaring it on the AndroidManifest without the android:process attribute made possible to run the automated tests on this new activity. One caveat: this must be done in the application package, not in the application’s test package. So this will add a little bit of turmoil to your application.

The possibility of running your application on the main thread may or may not be your case. If it isn’t, then you’ll have to find your own way. In this case, there’s one thing I would like to ask: if, by any chance, you solve this problem without the dirty trick, please leave a comment on this article. It may help people in the future, and I’ll gladly take links, if that’s the case.

You can get the sample code on github (as soon as I post it there).

Zooomr : ,

Random Activity

no comments

Installing Ubuntu 10.10 on a HP EliteBook 8440w

no comments

I was starring at a blank screen and asking myself “why is it blank?”. Then the sound of drums came out of the speakers. It hit me: the video card is the problem. I’ve been trying to install Ubuntu Desktop 10.10 on my new HP EliteBook 8440w for a a day, without success. Then I switched to Ubuntu Server 10.10 and the installation worked. Let me guide you through the process.

Installation

The first thing I’ve done was resizing the Windows Vista partition to make space to Ubuntu. This can be done using the tutorial on How-To geek: Resize a Partition for Free in Windows 7 or Vista. I still need Windows since the corporate world is plagued with this beast. If you don’t need to use it, you can get rid of it.

Next, you should download and burn the Ubuntu Server Edition 10.10 and reboot your computer with the disk on the tray (or the pen drive connected, if you choose to install with a pen drive.) After selecting the installation language, hit F6 and select nomodeset. Then proceed the installation. I chose not to use guided partitioning, and make the partition setup myself.

When the install process is completed, reboot your system into Ubuntu and login. To install the Ubuntu Desktop, issue the following commands:

sudo apt-get update && sudo apt-get install ubuntu-desktop

Now go get a mug of coffee. The desktop install will take a while.

Installing and configuring the NVidia Quadro FX 380m driver

Don’t reboot the computer yet. First you need to install and configure your video driver. To do this, run the following command:

sudo apt-get install nvidia-glx-180 && sudo nvidia-xconfig

Networking

I use network-manager to auto-configure my networks. The default Ubuntu Server installation comes without this tool, and the network is configured using the old debian scripts. To disable this, you’ll need to edit the configuration file, /etc/network/interfaces, commenting all lines except

auto lo
iface lo inet loopback

which means that your file will look something like this:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
#auto eth0
#iface eth0 inet dhcp

This should do the trick.

Now reboot your notebook and you should have the desktop installed and running.

Good luck, and YMMV.

Solved in 6 minutes (well, not really).

1 comment

Reading this post at RethinkDB I decided to take my chances at solving the problem they present to the candidates to work with them, which is:

Write a C function that reverses a singly-linked list.

Took me six minutes to solve it, plus circa 10 minutes to write the test, for a total of 16 minutes. It was not the best in class.

So I took more time to come up with another solution. It took me 30 minutes to solve it the best fashion I could.

Both solutions are shown below.

Solution #1

The first solution is based on recursion. Let’s see:

struct node *reverse_list(struct node *current, struct node *last) {
    struct node *next = current-&gt;next;
    current-&gt;next = last;
    if (next != NULL) {
        return reverse_list(next, current);
    } else {
        return current;
    }
}

Recursion is a beautiful thing. It lets you solve some tricky problems with a very simple code, no visible clutter in the code. Most of us would find it an elegant solution. And this is, for a duct tape solution, for a prototype.

Although elegant, recursion can easily blow your stack away. That’s because recursion uses the process stack a lot, to allocate stack frames. So, for some serious business this code is suboptimal. A few dozens of entries in this list, and the program would crash and burn.

What separates good programmers from the ones that excel at the craft (so I’ve been said) is the itch to fix their own code. And man, that was itching. So I spent some more time thinking and came up with a better idea, one that doesn’t use recursion.

Solution #2

The second solution is as follows.

struct node *reverse_list2(struct node *top) {
    struct node *next, *last = top, *first = top;
    while (top-&gt;next != NULL) {
        next = top-&gt;next;
        top-&gt;next = last;
        last = top;
        top = next;
    }
    top-&gt;next = last;
    first-&gt;next = NULL;
    return top;
}

So, 19 out of 20 miss this solution. I’ve actually have seen some pretty bad candidates in the industry, that make their resumes to appeal to the managers. “XML, CSS, Javascript, Java, C#” and, well, no real knowledge of the basics.

And this seems to be even worse. Some guys get really close and miss at the last point. Don’t get it? The guy attributed the right answer in a variable that will be destroyed at the block’s end. Which is the next line. Failed.

My take on this: being a master of the craft of programming pays off. Hard work pays off. Those who are reading should at least spend more time learning to do the real work instead of trying to fill their resumes with buzzwords.

My full answer is here. Thank you for you time and patience.

Extra bonus: I you came here looking for an answer to the riddle (which you shouldn’t), you can take the answer to the FizzBuzz for free.

Startup idea

no comments

If you are wondering how can you start a business, steal this (it’s for free, anyway): mobile development for startups. And if you can make this scale, you may turn it into a big cash cow for yourself.

If no one fill in this gap, I’ll start sometime when I’m less busy. But I have no idea how to make it scale.