Scalar udfs are generally quite slow and should be avoided.
Try to isolated the issue , what is performance like if you make it a sequential count ?
Also i notice that code is used is the clustered primary key , it could be page splitting on the inserts ?