1. optimized version
2. starttime and endtime