Function to measure time