Thursday, July 24, 2008

Python string concatenation performance

A small test revealed that "".join(listOfStrings) is not faster than plain +=. The .join() is slower. Using .append() and .join() is slower.

Time with .join():
real    0m2.908s
Time with +=:
real    0m1.742s
The test:
#!/usr/bin/env python

def combine(inc, count):
    text = ""
    for i in xrange(count):
        text += inc
    return len(text)

def combineByJoin(inc, count):
    text = []
    for i in xrange(count):
        text.append(inc)
    text = "".join(text)
    return len(text)

def main():
    inc = "a" * 10
    print combine(inc, 10000000)
    #print combineByJoin(inc, 10000000)

main()
Tested on Python 2.5.2.

Thursday, July 3, 2008

Stop disk clicking

If your disk is clicking on inactivity, it could be because it has too aggressive power management set. Each click will increase Load_Cycle_Count. Check that:
$ sudo smartctl -A /dev/sda | grep Load
To stop it, use hdparm:
$ sudo hdparm -B 254 /dev/sda
See how to force the hdparm setting on boot and resume: https://wiki.ubuntu.com/DanielHahler/Bug59695

Saturday, June 28, 2008

Form submit via jQuery

With jQuery it is easy to implement form submit by Ajax. This is how I do it:
$("#filter").submit(function(event) {
    event.preventDefault();
    event.stopPropagation();

    var params = {};
    $(":input", this).each(function() {
      params[this.name] = this.value;
    });

    $.get("/ajax/filter", params, aCallback);
});
A working example is the filter form on Jaký Byt: Mapa.