Friday, November 24, 2006

STL list size() method is slow

In STL list, the size() method is o(n), where n is number of elements in the list. The implementation of size() is by traversing the linked list and counting the nodes one by one. So, it sounds stupid.
I have written a simple program to test the performance of size() method in STL list and STL vector. For a STL list with 10M integers, it takes 0.17 sec. to get the size. However, for a STL vector with 10M integers, it takes 0.4 micro sec to get the size() in same machine.
There are some suggestions:
  1. Use vector instead of list.
  2. If the application need to check whether the list is empty or not, uses "list.emtpy()" instead of "list.size() != 0".
  3. use an extra counter variable to counting the size of a list.

Sunday, November 19, 2006

VMWare Server with ALSA DMix

In Linux, the sound-card drivers normally do not support concurrent access. This means when you playing MP3, your instant-chat program (e.g. gaim) will be mute. To solve this problem, there are some projects -- ALSA DMix, ESD, ARTSD, JACK.... However, most of them are not compatible each other and the applications needs to use the API provided by the specific project.

In my Ubuntu desktop, I use ALSA DMix with ESD approach. Most applications should support ALSA or ESD. However, VMWare support neither of them. VMWare only use native /dev/dsp device for sounding. After some searching on the web, we can use the LD_PRELOAD libossa.so trick.

After half day of try-and-error, the following is the summary of how to make VMWare work with ALSA:
  • make sure you installed the package "olsa-oss".
  • add suid bit to /usr/lib/libaoss.so by command chmod +s /usr/lib/libaoss.so
  • rename the /usr/lib/vmware/bin/vmware-vmx to vmware-vmx-real
  • create a shell script /usr/lib/vmware/bin/vmware-vmx with the following contents
#!/bin/sh
LD_PRELOAD=libaoss.so exec /usr/lib/vmware/bin/vmware-vmx-real $@
  • make sure the shell script is executable (i.e. chmod a+x /usr/lib/vmware/bin/vmware-vmx-real)
  • If your guest OS's disk image file is "single growable virtual disk" type with size large than 2G, you should use the vmware-vdiskmanager to split it into several files. Otherwise, you will encounter the problem of "File too large".
Related Link:
A Post about VMWare works with ALSA
ALSA DMix

Friday, November 10, 2006

Switch To Ubuntu

I have been used Linux as my primary desktop OS since 2001. At that time, I used Mandrake because it was easy to install. Then I switched to Debain because of its good package management and package update. Today, I decided to switch to Ubuntu.

Ubuntu is based on Debian's package management and adds user-friendly installation. No wonder Ubuntu is the most popular Linux distribution (http://distrowatch.com).

Today, I tried to install Ubuntu and don't encounter any difficulty. It can auto detect and configure all my hardware (sound card, video card, network card....). So, everything works properly!

One year ago, I have tried to install Ubuntu and I found it was not mature. So, I remained to use Debian. Now, Ubuntu 6.10 is much better. I love it!

Saturday, January 07, 2006

Does MTU matter?

Recently, I have changed my ISP to HKBN. I chose the plan BB10 which provide 10Mb upload and download speed. This is much better than ADSL. For such high bandwidth, I want to use my NSLU2 Linux box to setup a web server at my home.

However, there is some problems. I found that not every machine can access my web server. Ellen using Pacific Supernet cannot access the web server. Besides, a friend of mine using Netvigator faces the same problem. However, the W3C's HTML validate can access my web server to validate my HTML files. Moreover, my company can also access the server.

This is really strange. Today, I try to tackle the problem. I tried to set the server in DMZ at my router but fail. Then, I look for other setting in the router's configuration. I find the item "MTU". MTU stands for "maximum transmission unit". Currently setting is 1500 which is the maximum allowed value. I try to change the value smaller to 1400. It works! However, I want to ask, "Does MTU matter?".

Thursday, December 29, 2005

Install Ubuntu Linux Cont'

Today, I go to fine tune the Ubuntu Linux in my desktop. One disadvantage of Ubuntu comparing with Debian is Ubuntu supports less packages officially. For example, the SCIM, a Input Method modules support many languages, is not included in Ubuntu.

To install the SCIM, I includes the "Universe" packages pool in my Ubuntu and install the SCIM. The "Universe" pool contains some extra packages which is not officially support by Ubuntu team. After install the SCIM, I try to run the "Setup" of SCIM. However it can't run properly -- seg. fault. As a Debian user, this problem becomes the weak points of Ubuntu. In Debian, it officially support much more packages than Ubuntu. I think Ubuntu need to put more effort to support more packages.

Finally, I decide to install a Debian on my desktop and give up the fine tuning the Ubuntu. The installation of Debian is more complicated than Ubuntu. Fortunately, I am a experienced Debian User.

After installation complete, the XWindow can start correctly. However, my sound card doesn't work. To solve this, I need to install ALSA package via the "aptitude" (Note: aptitude is a Debian package manager). Moreover, the middle button of my mouse doesn't work and this is similar to the case of Ubuntu. The solution was mentioned yesterday's BLOG entry. Then, I install the SCIM and other Chinese related packages. The SCIM works properly!

Wednesday, December 28, 2005

Install Ubuntu Linux

According to DistroWatch, Ubuntu Linux is the most popular Linux distribution. Today, I want to experience how good it is.

The installation is quite simple and straight forward. It can detect all my hardware and can run up XWindow without any manual settings. My sound card and CD RW also works properly.

There is one problem on my mouse. The middle key doesn't works. With hours of investigation, I find the solution. The problem is due to the "psmouse" kernel module. It detects my mouse incorrectly. To solve the problem, the parameter "proto=imps" should be used while loading the kernel module. So, I create a file "psmouse" in "/etc/modprode.d" with the following contents:

options psmouse proto=imps 

Wednesday, December 14, 2005

CSV File Format

CSV stands for Comma Separated Values. It is a common file format for storing tabular data, e.g. spread sheet data. The file format of CSV is simple -- a text file, values are separated by comma (,) and rows are separated by newline. However, there is still some tricky in the format.

The tuck point is how to escape the comma in the values. If a value contains commas, the value should be quoted with double-quote ("). e.g.:

value one,"value two with ',' inside",value three
If a value contains a double-quote, the value should be quoted with double-quote and the double-quote in the value should be escaped with another double-quote. e.g.:
value one,"value two with '""' inside",value three
The specification of the CSV is described in RFC4180.

Even there is an RFC standard for CSV format, there are some deviation standard exists. In some applications, the comma and double-quote is escaped by a back-slash (\). Moreover, the character encoding is not stored in the file format and causes ambiguity. So, it is not trivial while using this format.