hi could anyone let me know an efficient way to measure time?

e.g. i want my program to stay in a loop for exactly 20 seconds, what would be the best way to do that?

thanks.