Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
LemonWiki共筆
Search
Search
Appearance
Log in
Personal tools
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Timer
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
Get the execution time of the script or sql query. == PHP == * [http://stackoverflow.com/questions/8310487/start-and-stop-a-timer-php Start and stop a timer PHP - Stack Overflow] * [http://stackoverflow.com/questions/6245971/accurate-way-to-measure-execution-times-of-php-scripts Accurate way to measure execution times of php scripts - Stack Overflow] == MySQL == Recording the time elapsed after the sql query was executed. <pre> 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'; </pre> == Linux command (BASH) == {{Linux}} console, {{Mac}} Terminal or Cygwin commands on {{Win}}<ref>[http://stackoverflow.com/questions/385408/get-program-execution-time-in-the-shell linux - Get program execution time in the shell - Stack Overflow]</ref><ref>[http://stackoverflow.com/questions/12199631/convert-seconds-to-hours-minutes-seconds bash - Convert seconds to hours, minutes, seconds - Stack Overflow]</ref> <pre> # 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 </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> == Windows command (DOS) == <pre> REM show the start time prompt $d $t $_$P$G REM do something ... REM show the finish time prompt $d $t $_$P$G </pre> or ...<ref>[https://stackoverflow.com/questions/673523/how-do-i-measure-execution-time-of-a-command-on-the-windows-command-line batch file - How do I measure execution time of a command on the Windows command line? - Stack Overflow]</ref><ref>[https://stackoverflow.com/questions/166044/sleeping-in-a-batch-file python - Sleeping in a batch file - Stack Overflow]</ref> <pre> 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% </pre> == Python == Using [https://docs.python.org/3/library/time.html#time.time time.time()]<ref>[http://stackoverflow.com/questions/7370801/measure-time-elapsed-in-python performance - Measure time elapsed in Python? - Stack Overflow]</ref> <pre> import time # start the timer start = time.time() # do something # stop the timer end = time.time() print("Elapsed time in seconds: ", end - start) </pre> == Javascript == <pre> 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); </pre> Reference * [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 == [https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#currentTimeMillis() Java.lang.System.currentTimeMillis()] <pre> # start the timer long start_time = System.currentTimeMillis(); # stop the timer System.out.println("\nElapsed time: " + (System.currentTimeMillis() - start_time) + " ms"); </pre> References <References /> [[Category:Programming]] [[Category:PHP]] [[Category:MySQL]] [[Category:Java]] [[Category:Python]] [[Category:Bash]] [[Category:Tool]]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki共筆:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Linux
(
edit
)
Template:Mac
(
edit
)
Template:Win
(
edit
)