fix potential memory overflow on gettime functions
use better code for elapsed time