cython code for fast computation of cost added

Now things are much faster