Timer: Difference between revisions
Appearance
No edit summary |
|||
| (One intermediate revision by the same user not shown) | |||
| Line 29: | Line 29: | ||
# expected result: | # expected result: | ||
# Elapsed time 00h:00m:02s | # Elapsed time 00h:00m:02s | ||
</pre> | |||
This following script measures elapsed time with millisecond precision | |||
<pre> | |||
# start the timer | |||
START=$(date +%s.%N) | |||
# sleep 2 seconds for testing purpose | |||
sleep 2s | |||
# stop the timer | |||
END=$(date +%s.%N) | |||
DIFF=$(awk "BEGIN {printf \"%.3f\", $END - $START}") | |||
# Use awk to break the seconds down into h/m/s while preserving the decimal part | |||
printf "Elapsed time %s\n" "$(awk -v t="$DIFF" 'BEGIN { | |||
h = int(t/3600) | |||
m = int((t%3600)/60) | |||
s = t%60 | |||
printf "%02dh:%02dm:%06.3fs", h, m, s | |||
}')" | |||
# expected result: | |||
# Elapsed time 00h:00m:02.002s | |||
</pre> | </pre> | ||
| Line 94: | Line 119: | ||
* [https://www.educative.io/edpresso/what-is-the-javascript-alternative-to-the-sleep-function What is the Javascript alternative to the sleep function?] | * [https://www.educative.io/edpresso/what-is-the-javascript-alternative-to-the-sleep-function What is the Javascript alternative to the sleep function?] | ||
* [https://www.jstips.co/zh_tw/javascript/extract-unix-timestamp-easily/ 在 JavaScript 簡單取得 unix timestamp] | |||
== Java == | == Java == | ||
Latest revision as of 11:23, 15 September 2026
Get the execution time of the script or sql query.
PHP[edit]
- Start and stop a timer PHP - Stack Overflow
- Accurate way to measure execution times of php scripts - Stack Overflow
MySQL[edit]
Recording the time elapsed after the sql query was executed.
SELECT @timer := CURRENT_TIMESTAMP(); SELECT SLEEP(2); /* sleep 2 seconds for testing purpose */ SELECT 'custom message' AS 'action', CURRENT_TIMESTAMP() AS 'finish time', @timer AS 'start time', TIMEDIFF(CURRENT_TIMESTAMP(), @timer) AS 'time elapsed';
Linux command (BASH)[edit]
Linux
console, macOS
Terminal or Cygwin commands on Win
[1][2]
# start the timer START=$(date +%s) # sleep 2 seconds for testing purpose sleep 2s # stop the timer END=$(date +%s) DIFF=$(( $END - $START )) printf "Elapsed time %02dh:%02dm:%02ds\n" $(($DIFF/3600)) $(($DIFF%3600/60)) $(($DIFF%60)) # expected result: # Elapsed time 00h:00m:02s
This following script measures elapsed time with millisecond precision
# start the timer
START=$(date +%s.%N)
# sleep 2 seconds for testing purpose
sleep 2s
# stop the timer
END=$(date +%s.%N)
DIFF=$(awk "BEGIN {printf \"%.3f\", $END - $START}")
# Use awk to break the seconds down into h/m/s while preserving the decimal part
printf "Elapsed time %s\n" "$(awk -v t="$DIFF" 'BEGIN {
h = int(t/3600)
m = int((t%3600)/60)
s = t%60
printf "%02dh:%02dm:%06.3fs", h, m, s
}')"
# expected result:
# Elapsed time 00h:00m:02.002s
Windows command (DOS)[edit]
REM show the start time prompt $d $t $_$P$G REM do something ... REM show the finish time prompt $d $t $_$P$G
REM start to record the start time set startTime=%time% REM do something ... REM show the start and finish time echo Start Time: %startTime% echo Finish Time: %time%
Python[edit]
Using time.time()[5]
import time
# start the timer
start = time.time()
# do something
# stop the timer
end = time.time()
print("Elapsed time in seconds: ", end - start)
Javascript[edit]
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// 1 second = 1000 ms
var startTimeInMs = Date.now();
await sleep(2000);
var endTimeInMs = Date.now();
var durationInMs = endTimeInMs - startTimeInMs;
console.log('durationInMs: ' + durationInMs);
var durationInSecond = durationInMs / 1000;
console.log('durationInSecond: ' + durationInSecond);
Reference
Java[edit]
Java.lang.System.currentTimeMillis()
# start the timer
long start_time = System.currentTimeMillis();
# stop the timer
System.out.println("\nElapsed time: " + (System.currentTimeMillis() - start_time) + " ms");
References
- ↑ linux - Get program execution time in the shell - Stack Overflow
- ↑ bash - Convert seconds to hours, minutes, seconds - Stack Overflow
- ↑ batch file - How do I measure execution time of a command on the Windows command line? - Stack Overflow
- ↑ python - Sleeping in a batch file - Stack Overflow
- ↑ performance - Measure time elapsed in Python? - Stack Overflow