in reply to Some code optimization

... The function itself is quite simple ...

I think it would help if you could post some (trimmed down) code that allows us to replicate the issue  (and in the process of trimming it you might perhaps find the solution yourself...)