optimizing segment comparison