Add timing code(slightly modified) from fofix.