speedup by rewrite to c++