Michal Čihař - Archive for March 27, 2007

Optimise your code

Especiall if there is chance it will be used on something like OpenWRT. Yesterday I installed Munin on my OpenWRT router. Now I can see that sometimes statistics drop to zero, because it didn't manage to catch them.

I looked for most CPU hungry plugins and those are df_inode and processes. I rememeber that I already had to rewrite df_inode, because it was damn slow, so I only used that version (it should be also somewhere in their tracker, but I can not find it right now).

The processes plugin was also quite easy to optimise, compare old and new code:

 # time find /proc -type d | grep [0-9]$ | tr -d "/proc/" | grep ^[0-9]*$ | wc -l
 real    0m 0.77s
 user    0m 0.28s
 sys     0m 0.39s

 # time ls -d /proc/[1-9]* | wc -l
 real    0m 0.06s
 user    0m 0.04s
 sys     0m 0.00s