Tuesday, November 24, 2009

Templates v subclasses v.2

A slightly more complicated test similar to the previous post. In this case the templated version doesn't have such a huge advantage.

Subclassed version:

#include <iostream>

using namespace std;

class mybaseclass {
public:

virtual int get_i() = 0;
};

class myclass : public mybaseclass {

public:

myclass(int in) {
i=in;
}

int get_i() {
i++;
return i;
}

int i;
};

class otherclass : public mybaseclass {

public:

otherclass(int in) {
i = in;
}

int get_i() {
return i*i;
}

int i;
};

int main(void) {
myclass *c = new myclass(2);

double sum=0;
for(size_t n=0;n<2000000000;n++) {

mybaseclass *cc = c;

sum += (static_cast<double>(cc->get_i())/2.2);

}
cout << sum << endl;
delete c;
}


Templated version:
#include <iostream>

using namespace std;

template<class t>
class myclass {

public:

myclass(int in) {
i=in;
}

inline int get_i() {
i++;
return i;
}

int i;
};

template<class t>
class otherclass {

public:

otherclass(int in) {
i = in;
}

inline int get_i() {
return i*i;
}

int i;
};

int main(void) {
myclass<int> *c = new myclass<int>(2);

double sum=0;
for(size_t n=0;n<2000000000;n++) {

sum += (static_cast<double>(c->get_i())/2.2);

}
cout << sum << endl;
delete c;
}



$ g++ small_classed.cpp -O3;time ./a.out
9.09091e+17

real 0m21.151s
user 0m20.909s
sys 0m0.072s


$ g++ small_templateclassed.cpp -O3;time ./a.out
9.09091e+17

real 0m20.736s
user 0m20.527s
sys 0m0.064s

Are templates faster than subclasses?

Are template classes faster than using a derived class? Intuition would tell me that the template class should be faster. However to test if this is the case I wrote the following small programs:

Subclassed version:

#include <iostream>

using namespace std;

class mybaseclass {
public:

virtual int get_i() = 0;
};

class myclass : public mybaseclass {

public:

myclass(int in) {
i=in;
}

int get_i() {
return i;
}

int i;
};

class otherclass : public mybaseclass {

public:

otherclass(int in) {
i = in;
}

int get_i() {
return i*i;
}

int i;
};

int main(void) {
myclass *c = new myclass(2);
long int sum=0;
for(size_t n=0;n<2000000000;n++) {
mybaseclass *cc = c;

sum += cc->get_i();

}
cout << sum << endl;
delete c;
}



Templated version:
#include <iostream>

using namespace std;

template<class t>
class myclass {

public:

myclass(int in) {
i=in;
}

int get_i() {
return i;
}

int i;
};

template<class t>
class otherclass {

public:

otherclass(int in) {
i = in;
}

int get_i() {
return i*i;
}

int i;
};

int main(void) {
myclass<int> *c = new myclass<int>(2);

long int sum=0;
for(size_t n=0;n<2000000000;n++) {

sum += c->get_i();

}
cout << sum << endl;
delete c;
}




$g++ small_templateclassed.cpp -O3;time ./a.out
4000000000

real 0m0.008s
user 0m0.000s
sys 0m0.001s


$ g++ small_classed.cpp -O3;time ./a.out
4000000000

real 0m5.908s
user 0m5.853s
sys 0m0.018s

My first effort was IO bound, but in this case it appears that templatized code is substantially faster.

Nabble thread is here.

Friday, November 20, 2009

Renicing synology nfsd so it has priority over other services

We needed nfs to have priority over other services on a synology DS509+. To do this we reniced the nfsd process. Synologies are basically just linux boxes. So enable ssh on the synology and ssh to it (ssh synology -lroot) use your normal admin password for the password. Then edit /usr/syno/etc/rc.d/S83nfsd.sh. Where it currently reads:

/sbin/portmap
/usr/sbin/nfsd
/usr/sbin/mountd -p 892
;;
stop)

change it to read:

/usr/sbin/nfsd
/usr/sbin/mountd -p 892
renice -10 `pidof nfsd`

;;
stop)

Monday, November 16, 2009

Commands used to install golang


Commands I used to install Go language (golang) on Ubuntu amd64:

sudo apt-get install mercurial bison gcc libc6-dev ed make
cd ~
export GOROOT=$HOME/go
export GOARCH=amd64
export GOOS=linux
mkdir $HOME/gobin
export GOBIN=$HOME/gobin
export PATH=$PATH:$GOBIN
hg clone -r release https://go.googlecode.com/hg/ $GOROOT
cd $GOROOT/src
./all.bash
To compile something make a test program e.g.:
package main

import "fmt"

func main() {
fmt.Printf("hello, world\n")
}

and compile with:

8g test.go ;8l test.8 ;./8.out

Version

Hi,

Probably did this before - but administered lost and found computers has made me need this info again.

Version of linux.
cat /proc/version


Gives you:
Linux version 2.6.28-15-generic (buildd@rothera) (gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4) ) #52-Ubuntu SMP Wed Sep 9 10:49:34 UTC 2009



And then you need the release info for the distribution.

lsb_release -a


Gives you:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 9.04
Release: 9.04
Codename: jaunty



And of course uname -a. Which is a start. Would love to hear other tips on determining the state of a box that has previously been abandoned.

Update: Naming of the actual machine and shit as well.

Running hostname should give you the name of the actual machine. Not entirely sure how this works when you have multiple "machines" kinda running off the same physical machine.
user@machine:~$ hostname
machine-name



You can then check this the other way round:
user@machine:~$ host name.domain.tld
ip address .in-addr.arpa domain name pointer name.domain.tld

Sunday, November 15, 2009

list installed packages on debian and uninstall those not correctly removed.

dpkg --get-selections

Will give you a list of packages currently installed on a debian system (possibly works for Ubuntu too?). You may see a few which are listed as "deinstall". They've not uninstalled correctly. To uninstall them use this command I found on the debian mailing lists to purge them:

dpkg --get-selections | grep deinstall | cut -f1 | xargs dpkg -P

Friday, November 13, 2009

Turning a Ubuntu install into a Window terminal server dumb terminal

I've written a quick script to term a clean Ubuntu (I'm using 9.04 netbook remix) into a dumb terminal to connect to a windows terminal server.

You can use it like this:

wget http://sgenomics.org/~new/term.tar
tar xvf term.tar
sudo ./termsvr MYTERMINALSERVERNAME MYDOMAINNAME


Under the hood, the script looks like this:

if [ $# -ne 2 ]
then
echo "Usage: `basename $0` "
exit 1
fi

apt-get -y --force-yes remove gnome*
apt-get update
apt-get install rdesktop
cp ./rc.local /etc/rc.local
sed "s/\*SERVERNAME\*/$1/" ./xinitrc > ./xinitrc.fixed0
sed "s/\*DOMAINNAME\*/$2/" ./xinitrc.fixed0 > ./xinitrc.fixed1
cp ./xinitrc.fixed1 /etc/X11/xinit/xinitrc

where xinitrc contains:

#!/bin/bash
# $Xorg: xinitrc.cpp,v 1.3 2000/08/17 19:54:30 cpqbld Exp $

# /etc/X11/xinit/xinitrc
#
# global xinitrc file, used by all X sessions started by xinit (startx)

while [ true ]
do
rdesktop -f *SERVERNAME* -d *DOMAINNAME* -u "" -k en-gb
done

# invoke global X session script
. /etc/X11/Xsession

and rc.local contains:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

startx

exit 0