Timer: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
(+dos)
No edit summary
Line 55: Line 55:


== Python ==
== Python ==
[http://stackoverflow.com/questions/7370801/measure-time-elapsed-in-python performance - Measure time elapsed in Python? - Stack Overflow]
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>


== Java ==
== Java ==

Revision as of 17:31, 8 April 2018

Get the execution time of the script or sql query.

PHP

MySQL

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)

Linux Os linux.png console, macOS icon_os_mac.png Terminal or Cygwin commands on Win Os windows.png [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

Windows command (DOS)

# show the start time
prompt $d $t $_$P$G

# do something

# show the finish time
prompt $d $t $_$P$G

or ...[3][4]

# recode the start time
set startTime=%time%

# do something

# show the start and finish time
echo Start Time: %startTime%
echo Finish Time: %time%

Python

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)

Java

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