Time a function: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎Hi-res version: Added library independant version)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(168 intermediate revisions by more than 100 users not shown)
Line 1:
{{task|Programming environment operations}}
[[Category:Date and time]]
Write a program which uses a timer (with the least granularity available on your system) to time how long a function takes to execute.
{{omit from|Batch File|No way to programmatically retrieving the current or elapsed time. Only human-readable formats available, which can't be parsed accurately.}}
{{omit from|GUISS}}
{{omit from|ML/I}}
 
;Task:
Whenever possible, use methods which measure only the processing time used by the current process; instead of the difference in [[system time]] between start and finish, which could include time used by other processes on the computer.
Write a program which uses a timer (with the least granularity available
on your system) to time how long a function takes to execute.
 
Whenever possible, use methods which measure only the processing time used
by the current process; instead of the difference in [[system time]]
between start and finish, which could include time used by
other processes on the computer.
 
This task is intended as a subtask for [[Measure relative performance of sorting algorithms implementations]].
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F do_work(x)
V n = x
L(i) 10000000
n += i
R n
 
F time_func(f)
V start = time:perf_counter()
f()
R time:perf_counter() - start
 
print(time_func(() -> do_work(100)))</syntaxhighlight>
 
=={{header|8051 Assembly}}==
 
Using a timer requires knowledge on two things: the oscillator frequency
(which limits the maximum precision) and the desired precision.
This code uses a common crystal of 11.0592MHz - but provides values
for a few others as examples.
This code also uses a precision of 4 bits (2^(-4) = 0.0625 seconds).
Those familiar with binary can think of this as a right shift of 4
of the multi-byte value, where the low 4 bits represent the fraction of a second,
and the remaining bits represent whole seconds.
The maximum time value depends on the number of bytes used and the precision.
For x bytes and p precision, the maximum value you can count to
is (256^x - 1) * 2^(-p).
 
<syntaxhighlight lang="asm">TC EQU 8 ; number of counter registers
TSTART EQU 08h ; first register of timer counter
TEND EQU TSTART + TC - 1 ; end register of timer counter
; Note: The multi-byte value is stored in Big-endian
 
; Some timer reloads
_6H EQU 085h ; 6MHz
_6L EQU 0edh
_12H EQU 00bh ; 12MHz
_12L EQU 0dbh
_110592H EQU 01eh ; 11.0592MHz
_110592L EQU 0ffh
 
; How to calculate timer reload (e.g. for 11.0592MHz):
; Note: 1 machine cycle takes 12 oscillator periods
; 11.0592MHz / 12 * 0.0625 seconds = 57,600 cycles = e100h
; ffffh - e100h = NOT e100h = 1effh
 
; assuming a 11.0592MHz crystal
TIMERH EQU _110592H
TIMERL EQU _110592L
 
;; some timer macros (using timer0)
start_timer macro
setb tr0
endm
stop_timer macro
clr tr0
endm
reset_timer macro
mov tl0, #TIMERL
mov th0, #TIMERH
endm
 
increment_counter macro ;; increment counter (multi-byte increment)
push psw
push acc
push 0 ; r0
mov r0, #TEND+1
setb c
inc_reg:
dec r0
clr a
addc a, @r0
mov @r0, a
jnc inc_reg_ ; end prematurally if the higher bytes are unchanged
cjne r0, #TSTART, inc_reg
inc_reg_:
; if the carry is set here then the multi byte value has overflowed
pop 0
pop acc
pop psw
endm
ORG RESET
jmp init
ORG TIMER0
jmp timer_0
 
timer_0: ; interrupt every 6.25ms
stop_timer ; we only want to time the function
reset_timer
increment_counter
start_timer
reti
init:
mov sp, #TEND
setb ea ; enable interrupts
setb et0 ; enable timer0 interrupt
mov tmod, #01h ; timer0 16-bit mode
reset_timer
; reset timer counter registers
clr a
mov r0, #TSTART
clear:
mov @r0, a
inc r0
cjne r0, #TEND, clear
start_timer
call function ; the function to time
stop_timer
; at this point the registers from TSTART
; through TEND indicate the current time
; multiplying the 8/16/24/etc length value by 0.0625 (2^-4) gives
; the elapsed number of seconds
; e.g. if the three registers were 02a0f2h then the elapsed time is:
; 02a0f2h = 172,274 and 172,274 * 0.0625 = 10,767.125 seconds
;
; Or alternatively:
; (high byte) 02h = 2 and 2 * 2^(16-4) = 8192
; (mid byte) a0h = 160 and 160 * 2^(8-4) = 2560
; (low byte) f2h = 242 and 242 * 2^(0-4) = 15.125
; 8192 + 2560 + 15.125 = 10,767.125 seconds
jmp $
function:
; do whatever here
ret
 
END
</syntaxhighlight>
 
=={{header|ACL2}}==
 
<syntaxhighlight lang="lisp">(time$ (nthcdr 9999999 (take 10000000 nil)))</syntaxhighlight>
 
Output (for Clozure):
<pre>; (EV-REC *RETURN-LAST-ARG3* ...) took
; 2.53 seconds realtime, 2.48 seconds runtime
; (160,001,648 bytes allocated).
(NIL)</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">BYTE RTCLOK1=$13
BYTE RTCLOK2=$14
BYTE PALNTSC=$D014
 
PROC Count(CARD max)
CARD i
 
FOR i=1 TO max DO OD
RETURN
 
CARD FUNC GetFrame()
CARD res
BYTE lsb=res,msb=res+1
 
lsb=RTCLOK2
msb=RTCLOK1
RETURN (res)
 
CARD FUNC FramesToMs(CARD frames)
CARD res
 
IF PALNTSC=15 THEN
res=frames*60
ELSE
res=frames*50
FI
RETURN (res)
 
PROC Main()
CARD ARRAY c=[1000 2000 5000 10000 20000 50000]
CARD beg,end,diff,diffMs
BYTE i
 
FOR i=0 TO 5
DO
PrintF("Count to %U takes ",c(i))
beg=GetFrame()
Count(c(i))
end=GetFrame()
diff=end-beg
diffMs=FramesToMs(diff)
PrintF("%U ms%E",diffMs)
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Time_a_function.png Screenshot from Atari 8-bit computer]
<pre>
Count to 1000 takes 50 ms
Count to 2000 takes 100 ms
Count to 5000 takes 300 ms
Count to 10000 takes 600 ms
Count to 20000 takes 1150 ms
Count to 50000 takes 3000 ms
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">with Ada.Calendar; use Ada.Calendar;
with Ada.Text_Io; use Ada.Text_Io;
 
Line 36 ⟶ 251:
Put_Line("Identity(4) takes" & Duration'Image(Time_It(Id_Access, 4)) & " seconds.");
Put_Line("Sum(4) takes:" & Duration'Image(Time_It(Sum_Access, 4)) & " seconds.");
end Query_Performance;</langsyntaxhighlight>
===Example===
Identity(4) takes 0.000001117 seconds.
Sum(4) takes: 0.000003632 seconds.
 
=={{header|Aime}}==
<syntaxhighlight lang="aime">integer
identity(integer x)
{
x;
}
 
 
integer
sum(integer c)
{
integer s;
 
s = 0;
while (c) {
s += c;
c -= 1;
}
 
s;
}
 
 
real
time_f(integer (*fp)(integer), integer fa)
{
date f, s;
time t;
 
s.now;
 
fp(fa);
 
f.now;
 
t.ddiff(f, s);
 
t.microsecond / 1000000r;
}
 
 
integer
main(void)
{
o_real(6, time_f(identity, 1));
o_text(" seconds\n");
o_real(6, time_f(sum, 1000000));
o_text(" seconds\n");
 
0;
}</syntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* program fcttime.s */
 
/* Constantes */
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
 
.equ N1, 1000000 @ loop number
.equ NBMEASURE, 10 @ measure number
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessError: .asciz "Error detected !!!!. \n"
szMessSep: .asciz "****************************\n"
szMessTemps: .ascii "Function time : "
sSecondes: .fill 10,1,' '
.ascii " s "
sMicroS: .fill 10,1,' '
.asciz " micros\n"
 
szCarriageReturn: .asciz "\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
.align 4
dwDebut: .skip 8
dwFin: .skip 8
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
adr r0,mult @ function address to measure
mov r1,#1 @ parameter 1 function
mov r2,#2 @ parameter 2 function
bl timeMesure
cmp r0,#0
blt 99f
adr r0,sum @ function address to measure
mov r1,#1
mov r2,#2
bl timeMesure
cmp r0,#0
blt 99f
b 100f
99:
@ error
ldr r0,iAdrszMessError
bl affichageMess
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
 
iAdrszMessError: .int szMessError
iAdrszCarriageReturn: .int szCarriageReturn
/**************************************************************/
/* examble function sum */
/**************************************************************/
/* r0 contains op 1 */
/* r1 contains op 2 */
sum:
push {lr} @ save registres
add r0,r1
100:
pop {lr} @ restaur registers
bx lr @ function return
 
/**************************************************************/
/* exemple execution multiplication */
/**************************************************************/
/* r0 contains op 1 */
/* r1 contains op 2 */
mult:
push {lr} @ save registres
mul r0,r1,r0
100:
pop {lr} @ restaur registers
bx lr @ function return
 
/**************************************************************/
/* Procedure for measuring the execution time of a routine */
/**************************************************************/
/* r0 contains the function address */
timeMesure:
push {r1-r8,lr} @ save registres
mov r4,r0 @ save function address
mov r5,r1 @ save param 1
mov r6,r2 @ save param 2
mov r8,#0
1:
ldr r0,iAdrdwDebut @ start time area
mov r1,#0
mov r7, #0x4e @ call system gettimeofday
svc #0
cmp r0,#0 @ error ?
blt 100f @ return error
ldr r7,iMax @ run number
mov r0,r5 @ param function 1
mov r1,r6 @ param function 2
2: @ loop
blx r4 @ call of the function to be measured
subs r7,#1 @ decrement run
bge 2b @ loop if not zero
@
ldr r0,iAdrdwFin @ end time area
mov r1,#0
mov r7, #0x4e @ call system gettimeofday
svc #0
cmp r0,#0 @ error ?
blt 100f @ return error
@ compute time
ldr r0,iAdrdwDebut @ start time area
//vidmemtit mesure r0 2
ldr r2,[r0] @ secondes
ldr r3,[r0,#4] @ micro secondes
ldr r0,iAdrdwFin @ end time area
ldr r1,[r0] @ secondes
ldr r0,[r0,#4] @ micro secondes
sub r2,r1,r2 @ secondes number
subs r3,r0,r3 @ microsecondes number
sublt r2,#1 @ if negative sub 1 seconde to secondes
ldr r1,iSecMicro
addlt r3,r1 @ and add 1000000 to microsecondes number
mov r0,r2 @ conversion secondes
ldr r1,iAdrsSecondes
bl conversion10
mov r0,r3 @ conversion microsecondes
ldr r1,iAdrsMicroS
bl conversion10
ldr r0,iAdrszMessTemps
bl affichageMess @ display message
add r8,#1
cmp r8,#NBMEASURE
ble 1b
ldr r0,iAdrszMessSep @ display separator
bl affichageMess
100:
pop {r1-r8,lr} @ restaur registers
bx lr @ function return
iMax: .int N1
iAdrdwDebut: .int dwDebut
iAdrdwFin: .int dwFin
iSecMicro: .int 1000000
iAdrsSecondes: .int sSecondes
iAdrsMicroS: .int sMicroS
iAdrszMessTemps: .int szMessTemps
iAdrszMessSep: .int szMessSep
 
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {r0,r1,r2,r7,lr} @ save registres
mov r2,#0 @ counter length
1: @ loop length calculation
ldrb r1,[r0,r2] @ read octet start position + index
cmp r1,#0 @ if 0 its over
addne r2,r2,#1 @ else add 1 in the length
bne 1b @ and loop
@ so here r2 contains the length of the message
mov r1,r0 @ address message in r1
mov r0,#STDOUT @ code to write to the standard output Linux
mov r7, #WRITE @ code call system "write"
svc #0 @ call systeme
pop {r0,r1,r2,r7,lr} @ restaur registers */
bx lr @ return
/******************************************************************/
/* Converting a register to a decimal */
/******************************************************************/
/* r0 contains value and r1 address area */
.equ LGZONECAL, 10
conversion10:
push {r1-r4,lr} @ save registers
mov r3,r1
mov r2,#LGZONECAL
1: @ start loop
bl divisionpar10 @ r0 <- dividende. quotient ->r0 reste -> r1
add r1,#48 @ digit
strb r1,[r3,r2] @ store digit on area
cmp r0,#0 @ stop if quotient = 0
subne r2,#1 @ previous position
bne 1b @ else loop
@ end replaces digit in front of area
mov r4,#0
2:
ldrb r1,[r3,r2]
strb r1,[r3,r4] @ store in area begin
add r4,#1
add r2,#1 @ previous position
cmp r2,#LGZONECAL @ end
ble 2b @ loop
mov r1,#' '
3:
strb r1,[r3,r4]
add r4,#1
cmp r4,#LGZONECAL @ end
ble 3b
100:
pop {r1-r4,lr} @ restaur registres
bx lr @return
/***************************************************/
/* division par 10 signé */
/* Thanks to http://thinkingeek.com/arm-assembler-raspberry-pi/*
/* and http://www.hackersdelight.org/ */
/***************************************************/
/* r0 dividende */
/* r0 quotient */
/* r1 remainder */
divisionpar10:
/* r0 contains the argument to be divided by 10 */
push {r2-r4} @ save registers */
mov r4,r0
mov r3,#0x6667 @ r3 <- magic_number lower
movt r3,#0x6666 @ r3 <- magic_number upper
smull r1, r2, r3, r0 @ r1 <- Lower32Bits(r1*r0). r2 <- Upper32Bits(r1*r0)
mov r2, r2, ASR #2 @ r2 <- r2 >> 2
mov r1, r0, LSR #31 @ r1 <- r0 >> 31
add r0, r2, r1 @ r0 <- r2 + r1
add r2,r0,r0, lsl #2 @ r2 <- r0 * 5
sub r1,r4,r2, lsl #1 @ r1 <- r4 - (r2 * 2) = r4 - (r0 * 10)
pop {r2-r4}
bx lr @ return
</syntaxhighlight>
{{out}}
<pre>
Function time : 0 s 16881 micros
Function time : 0 s 16728 micros
Function time : 0 s 16690 micros
Function time : 0 s 16904 micros
Function time : 0 s 16703 micros
Function time : 0 s 16686 micros
Function time : 0 s 16703 micros
Function time : 0 s 8240 micros
Function time : 0 s 7152 micros
Function time : 0 s 7143 micros
Function time : 0 s 7153 micros
****************************
Function time : 0 s 7153 micros
Function time : 0 s 7143 micros
Function time : 0 s 7153 micros
Function time : 0 s 7151 micros
Function time : 0 s 7151 micros
Function time : 0 s 7144 micros
Function time : 0 s 7153 micros
Function time : 0 s 7177 micros
Function time : 0 s 7143 micros
Function time : 0 s 7156 micros
Function time : 0 s 7154 micros
****************************
 
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">benchmark [
print "starting function"
pause 2000
print "function ended"
]</syntaxhighlight>
 
{{out}}
 
<pre>starting function
function ended
[benchmark] time: 2.005s</pre>
 
=={{header|AutoHotkey}}==
===System time===
Uses system time, not process time
<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox % time("fx")
Return
 
Line 57 ⟶ 601:
%function%(parameter)
Return ElapsedTime := A_TickCount - StartTime . " milliseconds"
}</langsyntaxhighlight>
=== Using QueryPerformanceCounter ===
QueryPerformanceCounter allows even more precision:
<syntaxhighlight lang="ahk">MsgBox, % TimeFunction("fx")
 
TimeFunction(Function, Parameters*) {
SetBatchLines, -1 ; SetBatchLines sets the speed of which every new line of coe is run.
DllCall("QueryPerformanceCounter", "Int64*", CounterBefore) ; Start the counter.
DllCall("QueryPerformanceFrequency", "Int64*", Freq) ; Get the frequency of the counter.
%Function%(Parameters*) ; Call the function with it's parameters.
DllCall("QueryPerformanceCounter", "Int64*", CounterAfter) ; End the counter.
 
; Calculate the speed of which it counted.
Return, (((CounterAfter - CounterBefore) / Freq) * 1000) . " milliseconds."
}
 
fx() {
Sleep, 1000
}</syntaxhighlight>
 
=={{header|BaCon}}==
The BaCon '''TIMER''' function keeps track of time spent running, in milliseconds (which is also the time unit used by '''SLEEP'''). This is not process specific, but a wall clock time counter which starts at 0 during process initialization. As BaCon can easily use external C libraries, process specific ''CLOCK_PROCESS_CPUTIME_ID'' '''clock_gettime''' could also be used.
 
<syntaxhighlight lang="freebasic">' Time a function
SUB timed()
SLEEP 7000
END SUB
 
st = TIMER
timed()
et = TIMER
PRINT st, ", ", et</syntaxhighlight>
 
{{out}}
<pre>prompt$ ./time-function
0, 7000</pre>
 
=={{header|BASIC}}==
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">DIM timestart AS SINGLE, timedone AS SINGLE, timeelapsed AS SINGLE
 
timestart = TIMER
SLEEP 1 'code or function to execute goes here
timedone = TIMER
 
'midnight check:
IF timedone < timestart THEN timedone = timedone + 86400
timeelapsed = timedone - timestart</syntaxhighlight>
 
See also: [[#BBC BASIC|BBC BASIC]], [[#PureBasic|PureBasic]].
 
=={{header|BASIC256}}==
<syntaxhighlight lang="basic256">call cont(10000000)
print msec; " milliseconds"
 
t0 = msec
call cont(10000000)
print msec+t0; " milliseconds"
end
 
subroutine cont(n)
sum = 0
for i = 1 to n
sum += 1
next i
end subroutine</syntaxhighlight>
 
=={{header|Batch File}}==
Granularity: hundredths of second.
<syntaxhighlight lang="batch file">
@echo off
Setlocal EnableDelayedExpansion
 
call :clock
 
::timed function:fibonacci series.....................................
set /a a=0 ,b=1,c=1
:loop
if %c% lss 2000000000 echo %c% & set /a c=a+b,a=b, b=c & goto loop
::....................................................................
 
call :clock
 
echo Function executed in %timed% hundredths of second
goto:eof
 
:clock
if not defined timed set timed=0
for /F "tokens=1-4 delims=:.," %%a in ("%time%") do (
set /A timed = "(((1%%a - 100) * 60 + (1%%b - 100)) * 60 + (1%%c - 100)) * 100 + (1%%d - 100)- %timed%"
)
goto:eof
</syntaxhighlight>
 
=={{header|BBC BASIC}}==
<syntaxhighlight lang="bbcbasic">start%=TIME:REM centi-second timer
REM perform processing
lapsed%=TIME-start%</syntaxhighlight>
 
=={{header|BQN}}==
To execute a function <code>F</code> once and get the amount of time it took to execute with value <code>v</code>, you can do this:
<syntaxhighlight lang="bqn">F •_timed v</syntaxhighlight>
<code>•_timed</code> is a system value that runs <code>F</code> a set number of times and returns the average runtime of the function. Here, since the left argument <code>𝕨</code> is omitted, it is run once.
 
The final result is in seconds.
 
Here are a few example runs:
<syntaxhighlight lang="bqn"> {0:1;𝕩×𝕊𝕩-1}•_timed 100
8.437800000000001e¯05
{0:1;𝕩×𝕊𝕩-1}•_timed 1000
0.000299545</syntaxhighlight>
 
 
=={{header|Bracmat}}==
<syntaxhighlight lang="bracmat">( ( time
= fun funarg t0 ret
. !arg:(?fun.?funarg)
& clk$:?t0
& !fun$!funarg:?ret
& (!ret.flt$(clk$+-1*!t0,3) s)
)
& ( fib
=
. !arg:<2&1
| fib$(!arg+-1)+fib$(!arg+-2)
)
& time$(fib.30)
)</syntaxhighlight>
Output:
<pre>1346269.5,141*10E0 s</pre>
 
=={{header|C}}==
Line 63 ⟶ 736:
On some system (like GNU/Linux) to be able to use the <tt>clock_gettime</tt> function you must link with the <tt>rt</tt> (RealTime) library.
 
<code>CLOCK_PROCESS_CPUTIME_ID</code> is preferred when available (eg. Linux kernel 2.6.12 up), being CPU time used by the current process. (<code>CLOCK_MONOTONIC</code> generally includes CPU time of unrelated processes, and may be drifted by <code>adjtime()</code>.)
<lang c>#include <stdio.h>
 
<syntaxhighlight lang="c">#include <stdio.h>
#include <time.h>
 
Line 75 ⟶ 750:
}
 
#ifdef CLOCK_PROCESS_CPUTIME_ID
#define CLOCKTYPE CLOCK_MONOTONIC
/* cpu time in the current process */
#define CLOCKTYPE CLOCK_PROCESS_CPUTIME_ID
#else
/* this one should be appropriate to avoid errors on multiprocessors systems */
#define CLOCKTYPE CLOCK_MONOTONIC
#endif
 
double time_it(int (*action)(int), int arg)
Line 96 ⟶ 776:
printf("sum (4) takes %lf s\n", time_it(sum, 4));
return 0;
}</langsyntaxhighlight>
 
=={{header|C++}}==
<lang cpp>#include <ctime>
#include <iostream>
using namespace std;
 
int identity(int x) { return x; }
int sum(int num) {
for (int i = 0; i < 1000000; i++)
num += i;
return num;
}
 
double time_it(int (*action)(int), int arg) {
clock_t start_time = clock();
action(arg);
clock_t finis_time = clock();
return ((double) (finis_time - start_time)) / CLOCKS_PER_SEC;
}
 
int main() {
cout << "Identity(4) takes " << time_it(identity, 4) << " seconds." << endl;
cout << "Sum(4) takes " << time_it(sum, 4) << " seconds." << endl;
return 0;
}</lang>
 
===Example===
Identity(4) takes 0 seconds.
Sum(4) takes 0.01 seconds.
 
=={{header|C sharp|C#}}==
Line 131 ⟶ 782:
Using Stopwatch.
 
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Threading;
Line 152 ⟶ 803:
Enumerable.Range(1, 10000).Where(x => x % 2 == 0).Sum(); // Sum even numers from 1 to 10000
}
}</langsyntaxhighlight>
 
Using DateTime.
 
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Threading;
Line 176 ⟶ 827:
Enumerable.Range(1, 10000).Where(x => x % 2 == 0).Sum(); // Sum even numers from 1 to 10000
}
}</langsyntaxhighlight>
 
Output:
DoSomething() took 1071,5408ms
 
=={{header|C++}}==
<pre>DoSomething() took 1071,5408ms</pre>
===Using <code>ctime</code>===
<syntaxhighlight lang="cpp">#include <ctime>
#include <iostream>
using namespace std;
 
int identity(int x) { return x; }
int sum(int num) {
for (int i = 0; i < 1000000; i++)
num += i;
return num;
}
 
double time_it(int (*action)(int), int arg) {
clock_t start_time = clock();
action(arg);
clock_t finis_time = clock();
return ((double) (finis_time - start_time)) / CLOCKS_PER_SEC;
}
 
int main() {
cout << "Identity(4) takes " << time_it(identity, 4) << " seconds." << endl;
cout << "Sum(4) takes " << time_it(sum, 4) << " seconds." << endl;
return 0;
}</syntaxhighlight>
 
Output:
<pre>
Identity(4) takes 0 seconds.
Sum(4) takes 0.01 seconds.
</pre>
 
===Using <code>std::chrono</code>===
 
<syntaxhighlight lang="cpp">
// Compile with:
// g++ -std=c++20 -Wall -Wextra -pedantic -O0 func-time.cpp -o func-time
 
#include <iostream>
#include <chrono>
 
template<typename f>
double measure(f func) {
auto start = std::chrono::steady_clock::now(); // Starting point
(*func)(); // Run the function
auto end = std::chrono::steady_clock::now(); // End point
return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); // By default, return time by milliseconds
}
 
/*
Test functions:
identity(): returns a number
addmillion(): add 1,000,000 to a number, one by one, using a for-loop
*/
 
int identity(int x) { return x; }
 
int addmillion(int num) {
for (int i = 0; i < 1000000; i++)
num += i;
return num;
}
 
int main() {
double time;
time = measure([](){ return identity(10); });
// Shove the function into a lambda function.
// Yeah, I couldn't think of any better workaround.
std::cout << "identity(10)\t\t" << time << " milliseconds / " << time / 1000 << " seconds" << std::endl; // Print it
time = measure([](){ return addmillion(1800); });
std::cout << "addmillion(1800)\t" << time << " milliseconds / " << time / 1000 << " seconds" << std::endl;
return 0;
}
</syntaxhighlight>
 
Output:
<pre>
identity(10) 0 milliseconds / 0 seconds
addmillion(1800) 4 milliseconds / 0.004 seconds
</pre>
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">
(defn fib []
(map first
(iterate
(fn [[a b]] [b (+ a b)])
[0 1])))
 
(time (take 100 (fib)))
</syntaxhighlight>
 
Output:
"Elapsed time: 0.028 msecs"
(0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181)
 
=={{header|Common Lisp}}==
Line 186 ⟶ 934:
Common Lisp provides a standard utility for performance measurement, [http://www.lispworks.com/documentation/HyperSpec/Body/m_time.htm time]:
 
<langsyntaxhighlight lang="lisp">CL-USER> (time (reduce #'+ (make-list 100000 :initial-element 1)))
Evaluation took:
0.151 seconds of real time
Line 193 ⟶ 941:
0 calls to %EVAL
0 page faults and
2,400,256 bytes consed.</langsyntaxhighlight>
 
(The example output here is from [[SBCL]].)
Line 201 ⟶ 949:
The functions [http://www.lispworks.com/documentation/HyperSpec/Body/f_get__1.htm get-internal-run-time] and [http://www.lispworks.com/documentation/HyperSpec/Body/f_get_in.htm get-internal-real-time] may be used to get time information programmatically, with at least one-second granularity (and usually more). Here is a function which uses them to measure the time taken for one execution of a provided function:
 
<langsyntaxhighlight lang="lisp">(defun timings (function)
(let ((real-base (get-internal-real-time))
(run-base (get-internal-run-time)))
Line 210 ⟶ 958:
CL-USER> (timings (lambda () (reduce #'+ (make-list 100000 :initial-element 1))))
17/500
7/250</langsyntaxhighlight>
 
=={{header|D}}==
<syntaxhighlight lang="d">import std.stdio, std.datetime;
<lang D>
 
int identity(int x) {
return x;
}
 
int sum(int num) {
foreach (i; 0 .. 100_000_000)
num += i;
return num;
}
 
double timeIt(int function(int) func, int arg) {
StopWatch sw;
sw.start();
func(arg);
sw.stop();
return sw.peek().usecs / 1_000_000.0;
}
 
void main() {
writefln("identity(4) takes %f6 seconds.", timeIt(&identity, 4));
writefln("sum(4) takes %f seconds.", timeIt(&sum, 4));
}</syntaxhighlight>
Output:<pre>identity(4) takes 0.0000016 seconds.
sum(4) takes 0.522065 seconds.</pre>
===Using Tango===
<syntaxhighlight lang="d">
import tango.io.Stdout;
import tango.time.Clock;
Line 241 ⟶ 1,016:
Stdout.format("Sum(4) takes {:f6} seconds",timeIt(&sum,4)).newline;
}
</syntaxhighlight>
</lang>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
[[File:DelphiTimerObject.png|frame|none]]
Here is a simple timer object that I use to time different parts of the code, to figure out which parts take the most time and are the best targets for optimization.
 
The object is based on the TPanel, which means the timer can be dropped on a form where it will display timing data whenever you want.
 
The time is controlled by four different commands: '''Reset, Start, Stop'''''Italic text'' and '''Display'''''Italic text''.
 
'''Reset'''. Reset zeros the timer.
'''Start'''. Starts the timer running.
'''Stop'''. Stops the timer.
'''Displays'''. Displays the current cumulative time since the first start.
 
Start and Stop can be moved around the code to control which parts are timed. You can even turn the timer on and off multiple times to messure the combined execution times of multiple different sections of code. You can also move the Start and Stop commands closer and closer together to zoom in on the part of the code that takes the most time to execute.
 
Finally, since the object is based on the TPanel component, the font, colors and layout can be made to look fancy for placement on the status bar of a program.
<syntaxhighlight lang="Delphi">
type TResolution=(rsSeconds,rsMiliSeconds);
 
type TCodeTimer=class(TPanel)
private
FResolution: TResolution;
public
WrkCount,TotCount: longint;
constructor Create(AOwner: TComponent); override;
procedure Reset;
procedure Start;
procedure Stop;
procedure Display;
published
property Resolution: TResolution read FResolution write FResolution default rsMiliSeconds;
end;
 
 
function GetHiResTick: integer;
var C: TLargeInteger;
begin
QueryPerformanceCounter(C);
Result:=C;
end;
 
 
 
 
constructor TCodeTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FResolution:=rsMiliSeconds;
end;
 
 
 
procedure TCodeTimer.Reset;
begin
WrkCount:=0;
TotCount:=0;
end;
 
 
procedure TCodeTimer.Start;
begin
WrkCount:=GetHiResTick;
end;
 
 
procedure TCodeTimer.Stop;
begin
TotCount:=TotCount+(GetHiResTick-WrkCount);
end;
 
procedure TCodeTimer.Display;
begin
if FResolution=rsSeconds then Caption:=FloatToStrF(TotCount/1000000,ffFixed,18,3)+' Sec.'
else Caption:=FloatToStrF(TotCount/1000,ffFixed,18,3)+' ms.'
end;
 
</syntaxhighlight>
{{out}}
<pre>
 
</pre>
 
=={{header|E}}==
Line 247 ⟶ 1,106:
{{trans|Java}} — E has no ''standardized'' facility for CPU time measurement; this {{works with|E-on-Java}}.
 
<langsyntaxhighlight lang="e">def countTo(x) {
println("Counting...")
for _ in 1..x {}
Line 263 ⟶ 1,122:
def finish := threadMX.getCurrentThreadCpuTime()
println(`Counting to $count takes ${(finish-start)//1000000}ms`)
}</langsyntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
func fua lim .
# this is interpreted
i = 1
while i <= lim
sum += i
i += 1
.
return sum
.
start = systime
print fua 1e8
print systime - start
#
fastfunc fub lim .
# this is compiled to wasm
i = 1
while i <= lim
sum += i
i += 1
.
return sum
.
start = systime
print fub 1e8
print systime - start
</syntaxhighlight>
 
=={{header|Elena}}==
{{trans|C#}}
ELENA 6.x :
<syntaxhighlight lang="elena">import system'calendar;
import system'routines;
import system'threading;
import system'math;
import extensions;
someProcess()
{
threadControl.sleep(1000);
new Range(0,10000).filterBy::(x => x.mod(2) == 0).summarize();
}
public program()
{
var start := now;
someProcess();
var end := now;
console.printLine("Time elapsed in msec:",(end - start).Milliseconds)
}</syntaxhighlight>
{{out}}
<pre>
Time elapsed in msec:1015
</pre>
 
=={{header|Elixir}}==
{{trans|Erlang}}
'''tc/1'''
<syntaxhighlight lang="elixir">iex(10)> :timer.tc(fn -> Enum.each(1..100000, fn x -> x*x end) end)
{236000, :ok}</syntaxhighlight>
'''tc/2'''
<syntaxhighlight lang="elixir">iex(11)> :timer.tc(fn x -> Enum.each(1..x, fn y -> y*y end) end, [1000000])
{2300000, :ok}</syntaxhighlight>
'''tc/3'''
<syntaxhighlight lang="elixir">iex(12)> :timer.tc(Enum, :to_list, [1..1000000])
{224000,
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
42, 43, 44, 45, 46, 47, 48, 49, ...]}</syntaxhighlight>
 
=={{header|EMal}}==
{{trans|VBA}}
<syntaxhighlight lang="emal">
fun identity = int by int x
int retval = 0
for int i = 0; i < 1000; ++i
retval = x
end
return retval
end
fun sum = int by int num
int t
for int j = 0; j < 1000; ++j
t = num
for int i = 0; i < 10000; i++
t = t + i
end
end
return t
end
int startTime, finishTime
startTime = time()
identity(1)
finishTime = time()
writeLine("1000 times Identity(1) takes " + (finishTime - startTime) + " milliseconds")
startTime = time()
sum(1)
finishTime = time()
writeLine("1000 times Sum(1) takes " + (finishTime - startTime) + " milliseconds")
</syntaxhighlight>
{{out}}
<pre>
1000 times Identity(1) takes 16 milliseconds
1000 times Sum(1) takes 6160 milliseconds
</pre>
 
=={{header|Erlang}}==
Erlang's timer module has three implementations of the tc function.
 
'''tc/1''' takes a 0-arity function and executes it:
<syntaxhighlight lang="erlang">
5> {Time,Result} = timer:tc(fun () -> lists:foreach(fun(X) -> X*X end, lists:seq(1,100000)) end).
{226391,ok}
6> Time/1000000. % Time is in microseconds.
0.226391
7> % Time is in microseconds.
</syntaxhighlight>
'''tc/2''' takes an n-arity function and its arguments:
<syntaxhighlight lang="erlang">
9> timer:tc(fun (X) -> lists:foreach(fun(Y) -> Y*Y end, lists:seq(1,X)) end, [1000000]).
{2293844,ok}
</syntaxhighlight>
'''tc/3''' takes a module name, function name and the list of arguments to the function:
<syntaxhighlight lang="erlang">
8> timer:tc(lists,seq,[1,1000000]).
{62370,
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,
23,24,25,26,27|...]}
</syntaxhighlight>
 
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">atom t
t = time()
some_procedure()
t = time() - t
printf(1,"Elapsed %f seconds.\n",t)</syntaxhighlight>
 
=={{header|F Sharp|F#}}==
The .Net framework provides a Stopwatch class which provides a performance counter.
<syntaxhighlight lang="fsharp">
open System.Diagnostics
let myfunc data =
let timer = new Stopwatch()
timer.Start()
let result = data |> expensive_processing
timer.Stop()
printf "elapsed %d ms" timer.ElapsedMilliseconds
result
</syntaxhighlight>
 
=={{header|Factor}}==
{{works with|Factor|0.98}}
<lang factor>[ 10000 iota sum drop ] time</lang>
<syntaxhighlight lang="factor">USING: kernel sequences tools.time ;
Output:
 
[ 10000 <iota> sum drop ] time</syntaxhighlight>
{{out}}
<pre>
Running time: 0.002888635 seconds
Line 280 ⟶ 1,297:
=={{header|Forth}}==
{{works with|GNU Forth}}
<langsyntaxhighlight lang="forth">: time: ( "word" -- )
utime 2>R ' EXECUTE
utime 2R> D-
<# # # # # # # [CHAR] . HOLD #S #> TYPE ." seconds" ;
 
1000 time: MS \ 1.000081 seconds ok</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Gfortran}} version 4.4.5 (Debian 4.4.5-8) on x86_64-linux-gnu
 
<syntaxhighlight lang="fortran">
c The subroutine to analyze
subroutine do_something()
c For testing we just do nothing for 3 seconds
call sleep(3)
return
end
 
c Main Program
program timing
integer(kind=8) start,finish,rate
call system_clock(count_rate=rate)
call system_clock(start)
c Here comes the function we want to time
call do_something()
call system_clock(finish)
write(6,*) 'Elapsed Time in seconds:',float(finish-start)/rate
return
end
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Function sumToLimit(limit As UInteger) As UInteger
Dim sum As UInteger = 0
For i As UInteger = 1 To limit
sum += i
Next
Return sum
End Function
 
Dim As Double start = timer
Dim limit As UInteger = 100000000
Dim result As UInteger = sumToLimit(limit)
Dim ms As UInteger = Int(1000 * (timer - start) + 0.5)
Print "sumToLimit("; Str(limit); ") = "; result
Print "took "; ms; " milliseconds to calculate"
Print
Print "Press any key to quit"
Sleep</syntaxhighlight>
 
{{out}}
<pre>
sumToLimit(100000000) = 5000000050000000
took 314 milliseconds to calculate
</pre>
 
=={{header|GAP}}==
<syntaxhighlight lang="gap"># Return the time passed in last function
time;</syntaxhighlight>
 
=={{header|Go}}==
===go test===
The Go command line tool <code>go test</code> includes [http://golang.org/pkg/testing/#hdr-Benchmarks benchmarking support].
Given a package with functions:
<syntaxhighlight lang="go">package empty
 
func Empty() {}
 
func Count() {
// count to a million
for i := 0; i < 1e6; i++ {
}
}</syntaxhighlight>
the following code, placed in a file whose name ends in <tt>_test.go</tt>, will time them:
<syntaxhighlight lang="go">package empty
 
import "testing"
 
func BenchmarkEmpty(b *testing.B) {
for i := 0; i < b.N; i++ {
Empty()
}
}
 
func BenchmarkCount(b *testing.B) {
for i := 0; i < b.N; i++ {
Count()
}
}</syntaxhighlight>
<code>go test</code> varies <code>b.N</code> to get meaningful resolution.
Example:
<pre>
$ go test -bench=.
testing: warning: no tests to run
PASS
BenchmarkEmpty 2000000000 0.30 ns/op
BenchmarkCount 10000 298734 ns/op
ok 3.642s
</pre>
The first number is the value of <code>b.N</code> chosen and the second the average time per iteration.
The <code>testing</code> package can optionally include memory use and throughput benchmarks.
 
There is also a [https://golang.org/x/tools/cmd/benchcmp standard tool] to compare the multiple benchmark outputs (installable via <code>go get golang.org/x/tools/cmd/benchcmp</code>).
 
===testing.Benchmark===
The benchmarking features of the <code>testing</code> package are exported for use within a Go program.
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"testing"
)
 
func empty() {}
 
func count() {
for i := 0; i < 1e6; i++ {
}
}
 
func main() {
e := testing.Benchmark(func(b *testing.B) {
for i := 0; i < b.N; i++ {
empty()
}
})
c := testing.Benchmark(func(b *testing.B) {
for i := 0; i < b.N; i++ {
count()
}
})
fmt.Println("Empty function: ", e)
fmt.Println("Count to a million:", c)
fmt.Println()
fmt.Printf("Empty: %12.4f\n", float64(e.T.Nanoseconds())/float64(e.N))
fmt.Printf("Count: %12.4f\n", float64(c.T.Nanoseconds())/float64(c.N))
}</syntaxhighlight>
{{out}}
<pre>
Empty function: 2000000000 0.80 ns/op
Count to a million: 2000 796071 ns/op
 
Empty: 0.7974
Count: 796071.6555
</pre>
 
===Alternative technique===
The <code>go test</code> command and the <code>testing</code> package are the preferred techniques for benchmarking or timing any Go code. Ignoring the well-tested and carefully crafted standard tools though, here is a simplistic alternative:
 
As the first line of the function you wish to time, use <tt>defer</tt> with an argument of <tt>time.Now()</tt> to print the elapsed time to the return of any function. For example, define the function <tt>from</tt> as shown below. It works because defer evaluates its function's arguments at the time the function is deferred, so the current time gets captured at the point of the defer. When the function containing the defer returns, the deferred <tt>from</tt> function runs, computes the elapsed time as a <tt>time.Duration</tt>, and prints it with standard formatting, which adds a nicely scaled unit suffix.
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"time"
)
 
func from(t0 time.Time) {
fmt.Println(time.Now().Sub(t0))
}
 
func empty() {
defer from(time.Now())
}
 
func count() {
defer from(time.Now())
for i := 0; i < 1e6; i++ {
}
}
 
func main() {
empty()
count()
}</syntaxhighlight>
Output:
<pre>
2us
643us
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
===CPU Timing===
<syntaxhighlight lang="groovy">import java.lang.management.ManagementFactory
import java.lang.management.ThreadMXBean
 
def threadMX = ManagementFactory.threadMXBean
assert threadMX.currentThreadCpuTimeSupported
threadMX.threadCpuTimeEnabled = true
 
def clockCpuTime = { Closure c ->
def start = threadMX.currentThreadCpuTime
c.call()
(threadMX.currentThreadCpuTime - start)/1000000
}</syntaxhighlight>
 
===Wall Clock Timing===
<syntaxhighlight lang="groovy">def clockRealTime = { Closure c ->
def start = System.currentTimeMillis()
c.call()
System.currentTimeMillis() - start
}</syntaxhighlight>
 
Test:
<syntaxhighlight lang="groovy">def countTo = { Long n ->
long i = 0; while(i < n) { i += 1L }
}
 
["CPU time":clockCpuTime, "wall clock time":clockRealTime].each { measurementType, timer ->
println '\n'
[100000000L, 1000000000L].each { testSize ->
def measuredTime = timer(countTo.curry(testSize))
println "Counting to ${testSize} takes ${measuredTime}ms of ${measurementType}"
}
}</syntaxhighlight>
 
Output:
<pre>Counting to 100000000 takes 23150.5484ms of CPU time
Counting to 1000000000 takes 233861.0991ms of CPU time
 
 
Counting to 100000000 takes 24314ms of wall clock time
Counting to 1000000000 takes 249005ms of wall clock time</pre>
 
=={{header|Halon}}==
<syntaxhighlight lang="halon">$t = uptime();
 
sleep(1);
 
echo uptime() - $t;</syntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import System.CPUTime (getCPUTime)
 
-- We assume the function we are timing is an IO monad computation
timeIt :: (Fractional c) => (a -> IO b) -> a -> IO c
timeIt action arg = do
do startTime <- getCPUTime
action arg
finishTime <- getCPUTime
return $ fromIntegral (finishTime - startTime) / 1000000000000
 
-- Version for use with evaluating regular non-monadic functions
timeIt'timeIt_ :: (Fractional c) => (a -> b) -> a -> IO c
timeIt'timeIt_ f = timeIt (\x -> f x (`seq` return ()) . f)</langsyntaxhighlight>
 
===Example===
Line 310 ⟶ 1,554:
 
=={{header|HicEst}}==
<langsyntaxhighlight HicEstlang="hicest">t_start = TIME() ! returns seconds since midnight
SYSTEM(WAIT = 1234) ! wait 1234 milliseconds
t_end = TIME()
 
WRITE(StatusBar) t_end - t_start, " seconds"</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
The function 'timef' takes as argument a procedure name and collects performance and timing information including run time (in milliseconds), garbage collection, and memory usage by region.
 
<syntaxhighlight lang="icon">procedure timef(f) #: time a function f
local gcol,alloc,used,size,runtime,header,x,i
 
title := ["","total","static","string","block"] # headings
collect() # start with collected memory (before baseline)
every put(gcol := [], -&collections) # baseline collections count
every put(alloc := [], -&allocated) # . total allocated space by region
every put(used := [], -&storage) # . currently used space by region - no total
every put(size := [], -&regions) # . current size of regions - no total
write("Performance and Timing measurement for ",image(f),":")
runtime := &time # base time
f()
write("Execution time=",&time-runtime," ms.")
 
every (i := 0, x := &collections) do gcol[i +:= 1] +:= x
every (i := 0, x := &allocated ) do alloc[i +:= 1] +:= x
every (i := 0, x := &storage ) do used[i +:= 1] +:= x
every (i := 0, x := &regions ) do size[i +:= 1] +:= x
 
push(gcol,"garbage collections:")
push(alloc,"memory allocated:")
push(used,"N/A","currently used:")
push(size,"N/A","current size:")
 
write("Memory Region and Garbage Collection Summary (delta):")
every (i := 0) <:= *!(title|gcol|alloc|used|size)
every x := (title|gcol|alloc|used|size) do {
f := left
every writes(f(!x,i + 3)) do f := right
write()
}
write("Note: static region values should be zero and may not be meaningful.")
return
end</syntaxhighlight>
 
Sample usage:<syntaxhighlight lang="icon">procedure main()
timef(perfectnumbers)
end
 
procedure perfectnumbers()
...</syntaxhighlight>
 
Sample output (from the [[Perfect_numbers#Icon_and_Unicon|Perfect Numbers]] task):
Performance and Timing measurement for procedure perfectnumbers:
Perfect numbers from 1 to 10000:
6
28
496
8128
Done.
Execution time=416 ms.
Memory Region and Garbage Collection Summary (delta):
total static string block
garbage collections: 2 0 0 2
memory allocated: 1247012 0 24 1246988
currently used: N/A 0 0 248040
current size: N/A 0 0 0
Note: static region values should be zero and may not be meaningful.
 
 
=={{header|Insitux}}==
Yes, non-transpiled Insitux really is this slow due to its original and ongoing commission: being shoehorned into a Roblox game.
<syntaxhighlight lang="insitux">(function measure
(let [start result end] [(time) (... . args) (time)])
(str result " took " (- end start) "ms"))
 
(function fib n
(if (< n 2) n
(+ (fib (dec n))
(fib (- n 2)))))
 
(measure fib 35)
;returns "9227465 took 26497ms"</syntaxhighlight>
 
=={{header|Ioke}}==
<langsyntaxhighlight lang="ioke">use("benchmark")
 
func = method((1..50000) reduce(+))
 
Benchmark report(1, 1, func)</langsyntaxhighlight>
 
=={{header|J}}==
Time and space requirements are tested using verbs obtained through the Foreign conjunction (<ttcode>!:</ttcode>). <ttcode>6!:2</ttcode> returns time required for execution, in floating-point measurement of seconds. <ttcode>7!:2</ttcode> returns a measurement of space required to execute. Both receive as input a sentence for execution. The verb <code>timespacex</code> combines these and is available in the standard library.
 
<br>When the [http://www.jsoftware.com/help/dictionary/dmcapdot.htm Memoize] feature or similar techniques are used, execution time and space can both be affected by prior calculations.
There's also <code>timex</code> (which is defined as <code>6!:2</code>, but easier for some people to remember) which measures only the execution time. Interestingly, timex typically measures slightly larger times than timespacex. This is likely due to the difference between cold cache (timex) and warm cache (timespacex) -- in timespacex, the modularity of its design means that two runs of the code are performed, once to measure space use the other to measure time use.
 
When the [http://www.jsoftware.com/help/dictionary/dmcapdot.htm Memoize] feature or similar techniques are used, execution time and space can both be affected by prior calculations.
===Example===
<langsyntaxhighlight lang="j"> (6!:2 , 7!:2) '|: 50 50 50 $ i. 50^3' (6!:2,7!:2) '|: 50 50 50 $ i. 50^3'
0.0014169 2.09875e6
0.00387912 1.57414e6</lang>
timespacex '|: 50 50 50 $ i. 50^3'
0.0014129 2.09875e6
timex '|: 50 50 50 $ i. 50^3'
0.0015032</syntaxhighlight>
 
=={{header|Janet}}==
<syntaxhighlight lang="clojure">(defmacro time
"Print the time it takes to evaluate body to stderr.\n
Evaluates to body."
[body]
(with-syms [$start $val]
~(let [,$start (os/clock)
,$val ,body]
(eprint (- (os/clock) ,$start))
,$val)))
 
(time (os/sleep 0.5))</syntaxhighlight>
{{out}}
<pre>0.500129</pre>
 
=={{header|Java}}==
If you're looking for a quick way to calculate the duration of a few lines of code you can utilize the <code>System.currentTimeMillis</code> method.
<syntaxhighlight lang="java">
long start = System.currentTimeMillis();
/* code you want to time, here */
long duration = System.currentTimeMillis() - start;
</syntaxhighlight>
<br />
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java">import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
 
Line 357 ⟶ 1,709:
System.out.println("Done!");
}
}</langsyntaxhighlight>
 
Measures real time rather than CPU time:
{{works with|Java|(all versions)}}
 
<langsyntaxhighlight lang="java"> public static void main(String[] args){
long start, end;
start = System.currentTimeMillis();
Line 373 ⟶ 1,725:
System.out.println("Counting to 1000000000 takes "+(end-start)+"ms");
 
}</langsyntaxhighlight>
Output:
Counting...
Done!
Counting to 100000000 takes 370ms
Counting...
Done!
Counting to 1000000000 takes 3391ms
 
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">
function test() {
let n = 0
for(let i = 0; i < 1000000; i++){
n += i
}
}
 
let start = new Date().valueOf()
test()
let end = new Date().valueOf()
 
console.log('test() took ' + ((end - start) / 1000) + ' seconds') // test() took 0.001 seconds
</syntaxhighlight>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">clock
1 1023 [dup +] times pop
clock swap -.</syntaxhighlight>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia"># v0.6.0
 
function countto(n::Integer)
i = zero(n)
println("Counting...")
while i < n
i += 1
end
println("Done!")
end
 
@time countto(10 ^ 5)
@time countto(10 ^ 10)</syntaxhighlight>
 
{{out}}
<pre>Counting...
Done!
Counting to 100000000 takes 370ms
Counting...
Done!
0.000109 seconds (15 allocations: 400 bytes)
Counting to 1000000000 takes 3391ms</pre>
Counting...
Done!
0.000127 seconds (15 allocations: 400 bytes)</pre>
 
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="scala">// version 1.1.2
// need to enable runtime assertions with JVM -ea option
 
import java.lang.management.ManagementFactory
import java.lang.management.ThreadMXBean
 
fun countTo(x: Int) {
println("Counting...");
(1..x).forEach {}
println("Done!")
}
 
fun main(args: Array<String>) {
val counts = intArrayOf(100_000_000, 1_000_000_000)
val threadMX = ManagementFactory.getThreadMXBean()
assert(threadMX.isCurrentThreadCpuTimeSupported)
threadMX.isThreadCpuTimeEnabled = true
for (count in counts) {
val start = threadMX.currentThreadCpuTime
countTo(count)
val end = threadMX.currentThreadCpuTime
println("Counting to $count takes ${(end-start)/1000000}ms")
}
}</syntaxhighlight>
This is a typical result (sometimes the second figure is only about 1400ms - no idea why)
{{out}}
<pre>
Counting...
Done!
Counting to 100000000 takes 179ms
Counting...
Done!
Counting to 1000000000 takes 3527ms
</pre>
 
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">local(start = micros)
loop(100000) => {
'nothing is outout because no autocollect'
}
'time for 100,000 loop repititions: '+(micros - #start)+' microseconds'</syntaxhighlight>
 
=={{header|Lingo}}==
<syntaxhighlight lang="lingo">on testFunc ()
repeat with i = 1 to 1000000
x = sqrt(log(i))
end repeat
end</syntaxhighlight>
 
<syntaxhighlight lang="lingo">ms = _system.milliseconds
testFunc()
ms = _system.milliseconds - ms
put "Execution time in ms:" && ms
-- "Execution time in ms: 983"</syntaxhighlight>
 
=={{header|Logo}}==
Line 387 ⟶ 1,842:
This is not an ideal method; Logo does not expose a timer (except for the WAIT command) so we use the Unix "date" command to get a second timer.
 
<langsyntaxhighlight lang="logo">to time
output first first shell "|date +%s|
end
Line 396 ⟶ 1,851:
end
 
elapsed [wait 300] ; 5 seconds elapsed</langsyntaxhighlight>
 
=={{header|MathematicaLua}}==
<syntaxhighlight lang="lua">function Test_Function()
<lang Mathematica>AbsoluteTiming[x];</lang>
for i = 1, 10000000 do
local s = math.log( i )
s = math.sqrt( s )
end
end
 
t1 = os.clock()
Test_Function()
t2 = os.clock()
 
print( os.difftime( t2, t1 ) )</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
We use Profiler to reset timer, and Timecount to read time in milliseconds as a double, with nanoseconds for resolution. Internal use of QueryPerformanceCounter from Windows Api.
In this example we get times for use of same module with different variable types. sum=limit-limit make sum 0 to the same type of limit,and using n=sum and n++ we make n=1 using same type as sum.
 
10000% is Integer 16bit
 
10000& is Long 32bit
 
10000@ is Decimal
 
10000# is Currency
 
10000~ is Float
 
10000 is Double (default)
 
10000&& is long long (64bit integer)
 
255ub is Byte type (unsigned value, 0 to 255)
 
Although parameter limit take the type Byte from the argument passed, the limit-limit converted to integer, so sum and n get type integer, so the loop use integers 16bit. So we use the sumtolimit2. The n-! change sign, and this cause overflow for byte value, so this removed from the sumtolimit2.
 
Function/Module Parameters in M2000 without explicitly assign type take the type from stack of values which a Read statement (which Interpreter insert to code) read from there. This type can't change for the run of module or function. We can define a parameter as variant if we want to allow changes of the type.
 
<syntaxhighlight lang="m2000 interpreter">
Module Checkit {
Module sumtolimit (limit) {
sum=limit-limit
n=sum
rem print type$(n), type$(sum), type$(limit)
n++
while limit {sum+=limit*n:limit--:n-!}
}
Module sumtolimit2 (limit) {
byte sum, n
n++
while limit {sum++:limit--}
}
Cls ' clear screen
Profiler
sumtolimit 10000%
Print TimeCount
Profiler
sumtolimit 10000&
Print TimeCount
Profiler
sumtolimit 10000#
Print TimeCount
Profiler
sumtolimit 10000@
Print TimeCount
Profiler
sumtolimit 10000~
Print TimeCount
Profiler
sumtolimit 10000
Print TimeCount
Profiler
sumtolimit 10000&&
Print TimeCount
Profiler
sumtolimit 255ub
Print TimeCount
Profiler
sumtolimit2 255ub
Print TimeCount
}
Checkit</syntaxhighlight>
 
=={{header|Maple}}==
The built-in command CodeTools:-Usage can compute the "real" time for the length of the computation or the "cpu" time for the computation. The following examples find the real time and cpu time for computing the integer factors for 32!+1.
<syntaxhighlight lang="maple">CodeTools:-Usage(ifactor(32!+1), output = realtime, quiet);</syntaxhighlight>
<syntaxhighlight lang="maple">CodeTools:-Usage(ifactor(32!+1), output = cputime, quiet);</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">AbsoluteTiming[x];</syntaxhighlight>
where x is an operation. Example calculating a million digits of Sqrt[3]:
<langsyntaxhighlight Mathematicalang="mathematica">AbsoluteTiming[N[Sqrt[3], 10^6]]</langsyntaxhighlight>
{{out}}
gives:
<langsyntaxhighlight Mathematicalang="mathematica">{0.000657, 1.7320508075688772935274463......}</langsyntaxhighlight>
First elements if the time in seconds, second elements if the result from the operation. Note that I truncated the result.
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">f(n) := if n < 2 then n else f(n - 1) + f(n - 2)$
 
/* First solution, call the time function with an output line number, it gives the time taken to compute that line.
Here it's assumed to be %o2 */
f(24);
46368
 
time(%o2);
[0.99]
 
/* Second solution, change a system flag to print timings for all following lines */
showtime: true$
 
f(24);
Evaluation took 0.9400 seconds (0.9400 elapsed)
46368</syntaxhighlight>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">start = time
for i in range(1,100000)
end for
duration = time - start
print "Process took " + duration + " seconds"</syntaxhighlight>
{{out}}
<pre>
Process took 0.312109 seconds
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import times, strutils
proc doWork(x: int) =
var n = x
for i in 0..10000000:
n += i
template time(statement: untyped): float =
let t0 = cpuTime()
statement
cpuTime() - t0
echo "Time = ", time(doWork(100)).formatFloat(ffDecimal, precision = 3), " s"</syntaxhighlight>
 
{{out}}
Compiled in debug mode (no options).
<pre>Time = 0.046 s</pre>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let time_it action arg =
let start_time = Sys.time () in
ignore (action arg);
let finish_time = Sys.time () in
finish_time -. start_time</langsyntaxhighlight>
 
===Example===
Line 422 ⟶ 2,013:
Sum(4) takes 0.084005 seconds.
- : unit = ()
 
=={{header|Oforth}}==
 
bench allows to calculate how long a runnable takes to execute.
 
Result is microseconds.
 
It uses difference between system time, not processing time.
 
{{Out}}
<pre>
>#[ 0 1000 seq apply(#+) ] bench .
267
500500 ok
</pre>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
%% returns milliseconds
fun {TimeIt Proc}
Line 442 ⟶ 2,048:
{FoldL {List.number 1 1000000 1} Number.'+' 4 _}
end}
}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
This version, by default, returns just the CPU time used by gp, not the delta of wall times. PARI can be compiled to use wall time if you prefer: configure with <code>--time=ftime</code> instead of <code>--time=
getrusage</code>, <code>--time=clock_gettime</code>, or <code>--time=times</code>. See Appendix A, section 2.2 of the User's Guide to PARI/GP.
<syntaxhighlight lang="parigp">time(foo)={
foo();
gettime();
}</syntaxhighlight>
 
Alternate version:
{{works with|PARI/GP|2.6.2+}}
<syntaxhighlight lang="parigp">time(foo)={
my(start=getabstime());
foo();
getabstime()-start;
}</syntaxhighlight>
 
=={{header|Perl}}==
Example of using the built-in Benchmark core module - it compares two versions of recursive factorial functions:
<langsyntaxhighlight lang="perl">use Benchmark;
use Memoize;
 
Line 463 ⟶ 2,085:
'fac2' => sub { fac2(50) },
});
Benchmark::cmpthese($result);</langsyntaxhighlight>
Output:
Benchmark: timing 100000 iterations of fac1, fac2...
<pre>
fac1: 6 wallclock secs ( 5.45 usr + 0.00 sys = 5.45 CPU) @ 18348.62/s (n=100000)
Benchmark: timing 100000 iterations of fac1, fac2...
fac1 fac2: 61 wallclock secs ( 50.4584 usr + 0.00 sys = 50.4584 CPU) @ 18348119047.62/s (n=100000)
Rate fac1 fac2
fac2: 1 wallclock secs ( 0.84 usr + 0.00 sys = 0.84 CPU) @ 119047.62/s (n=100000)
fac1 18349/s -- -85%
Rate fac1 fac2
fac1 fac2 18349119048/s 549% -- -85%
fac2 119048/s 549% --
</pre>
 
Example without using Benchmark:
<langsyntaxhighlight lang="perl">sub cpu_time {
my ($user,$system,$cuser,$csystem) = times;
$user + $system
Line 500 ⟶ 2,120:
 
printf "Sum(4) takes %f seconds.\n", time_it(\&sum, 4);
# outputs "Sum(4) takes 0.280000 seconds."</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
Measures wall-clock time. On Windows the resolution is about 15ms. The elapsed function makes it more human-readable, eg elapsed(720) yields "12 minutes".
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">identity</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">x</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">total</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">num</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">100_000_000</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">num</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">num</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">time_it</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">funcname</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_routine_info</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)[</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s(4) = %d, taking %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">funcname</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">time_it</span><span style="color: #0000FF;">(</span><span style="color: #000000;">identity</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">time_it</span><span style="color: #0000FF;">(</span><span style="color: #000000;">total</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
identity(4) = 4, taking 0s
total(4) = 50000004, taking 6.8s
</pre>
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="phixmonti">def count
for drop endfor
enddef
 
1000000 count
msec dup var t0 print " seconds" print nl
 
10000000 count
msec t0 - print " seconds" print</syntaxhighlight>
 
=={{header|Picat}}==
Picat had some built-in timing functions/predicates:
* <code>time/1</code>: reports the time since start of execution. (The related <code>time2/1</code> also reports the backtracks for CP problems.)
* <code>statistics(runtime,[ProgTime,LastTime])</code>: <code>ProgTime</code> is the ms since program started, <code>LastTime</code> is the ms since last call to <code>statistics(runtime,_)</code>. It can be used to create user defined time predicates/functions, as show in <code>time1b/1</code>.
 
<syntaxhighlight lang="picat">import cp.
 
go =>
println("time/1 for 201 queens:"),
time2(once(queens(201,_Q))),
nl,
 
% time1b/1 is a used defined function (using statistics/2)
Time = time1b($once(queens(28,Q2))),
println(Q2),
printf("28-queens took %dms\n", Time),
nl.
 
% N-queens problem.
% N: number of queens to place
% Q: the solution
queens(N, Q) =>
Q=new_list(N),
Q :: 1..N,
all_different(Q),
all_different([$Q[I]-I : I in 1..N]),
all_different([$Q[I]+I : I in 1..N]),
solve([ffd,split],Q).
 
% time1b/1 is a function that returns the time (ms)
time1b(Goal) = T =>
statistics(runtime, _),
call(Goal),
statistics(runtime, [_,T]).</syntaxhighlight>
 
{{out}}
<pre>time/1 for 201 queens:
CPU time 0.049 seconds. Backtracks: 0
 
[1,3,5,23,13,4,21,7,14,26,24,19,6,20,18,28,8,27,2,10,25,17,9,16,12,15,11,22]
28-queens took 0ms</pre>
 
=={{header|PicoLisp}}==
There is a built-in function '[http://software-lab.de/doc/refB.html#bench bench]'
for that. However, it measures wall-clock time, because for practical purposes
the real time needed by a task (including I/O and communication) is more meaningful.
There is another function, '[http://software-lab.de/doc/refT.html#tick tick]', which
also measures user time, and is used by the profiling tools.
<syntaxhighlight lang="picolisp">: (bench (do 1000000 (* 3 4)))
0.080 sec
-> 12</syntaxhighlight>
 
=={{header|Pike}}==
Shows CPU time used, not including any automatic gc passes. Explicit
calls to gc() are included though. This example uses the convenience
function gauge(), but it could also be done manually with
gethrvtime() in ms or ns resolution.
<syntaxhighlight lang="pike">
void get_some_primes()
{
int i;
while(i < 10000)
i = i->next_prime();
}
 
void main()
{
float time_wasted = gauge( get_some_primes() );
write("Wasted %f CPU seconds calculating primes\n", time_wasted);
}
</syntaxhighlight>
{{Out}}
<pre>
Wasted 0.014 CPU seconds calculating primes
</pre>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">declare (start_time, finish_time) float (18);
<lang PL/I>
declare (start_time, finish_time) float (18);
 
start_time = secs();
Line 518 ⟶ 2,256:
 
/* Note: using the SECS function takes into account the clock */
/* going past midnight. */</syntaxhighlight>
</lang>
 
=={{header|PicoLispPowerShell}}==
<syntaxhighlight lang="powershell">
There is a built-in function '[http://software-lab.de/doc/refB.html#bench bench]'
function fun($n){
for that. However, it measures wall-clock time, because for practical purposes
$res = 0
the real time needed by a task (including I/O and communication) is more meaningful.
if($n -gt 0) {
There is another function, '[http://software-lab.de/doc/refT.html#tick tick]', which
1..$n | foreach{
also measures user time, and is used by the profiling tools.
$a, $b = $_, ($n+$_)
<lang PicoLisp>: (bench (do 1000000 (* 3 4)))
$res += $a + $b
0.080 sec
}
-> 12</lang>
 
}
$res
}
"$((Measure-Command {fun 10000}).TotalSeconds) Seconds"
</syntaxhighlight>
<b>Output:</b>
<pre>
0.820712 Seconds
</pre>
 
=={{header|PureBasic}}==
===Built in timer===
This version uses the built in timer, on Windows it has an accuracy of ~10-15 msec.
<langsyntaxhighlight Purebasiclang="purebasic">Procedure Foo(Limit)
Protected i, palindromic, String$
For i=0 To Limit
Line 556 ⟶ 2,302:
PrintN("and "+Str(cnt)+" are palindromic.")
Print("Press ENTER to exit."): Input()
EndIf</langsyntaxhighlight>
 
<pre>Starting timing of a calculation,
Starting timing of a calculation,
for this we test how many of 0-1000000 are palindromic.
for this we test how many of 0-1000000 are palindromic.
The function need 577 msec,
The function need 577 msec,
and 1999 are palindromic.
and 1999 are palindromic.
Press ENTER to exit.</pre>
Press ENTER to exit.
 
===Hi-res version===
{{libheader|Droopy}}
 
This version uses a hi-res timer, but it is Windows only.
<langsyntaxhighlight PureBasiclang="purebasic">If OpenConsole()
Define Timed.f, cnt
PrintN("Starting timing of a calculation,")
Line 579 ⟶ 2,327:
PrintN("and "+Str(cnt)+" are palindromic.")
Print("Press ENTER to exit."): Input()
EndIf</langsyntaxhighlight>
<pre>Starting timing of a calculation,
for this we test how many of 0-1000000 are palindromic.
The function need 604.341 msec,
and 1999 are palindromic.
Press ENTER to exit.</pre>
 
Starting timing of a calculation,
for this we test how many of 0-1000000 are palindromic.
The function need 604.341 msec,
and 1999 are palindromic.
Press ENTER to exit.
 
This version still relies on the Windows API but does not make use of any additional libraries.
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.f ticksHQ(reportIfPresent = #False)
Static maxfreq.q
Protected T.q
Line 617 ⟶ 2,365:
PrintN("and " + Str(cnt) + " are palindromic.")
Print("Press ENTER to exit."): Input()
EndIf</langsyntaxhighlight>
 
Sample output:
<pre> Starting timing of a calculation,
for this we test how many of 0-1000000 are palindromic.
The function need 174.811 msec,
and 1999 are palindromic.</pre>
 
=={{header|Python}}==
Line 628 ⟶ 2,377:
 
'''Note:''' There is an overhead in executing a function that does nothing.
<langsyntaxhighlight lang="python">import sys, timeit
def usec(function, arguments):
modname, funcname = __name__, function.__name__
Line 644 ⟶ 2,393:
raise
 
from math import pow
def nothing(): pass
def identitynothing(x): return x</lang>pass
def identity(x): return x</syntaxhighlight>
 
===Example===
>>> print (usec(nothing, []))
1.7
>>> print (usec(identity, [1]))
2.2
>>> print (usec(pow, (2, 100)))
3.3
>>> print ([usec(qsort, (range(n),)) for n in range(10)])
[2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]
using ''qsort()'' from [[Quicksort#Python|Quicksort]]. Timings show that the implementation of ''qsort()'' has quadratic dependence on sequence length ''N'' for already sorted sequences (instead of ''O(N*log(N))'' in average).
 
=={{header|Quackery}}==
 
<code>time</code> returns system time since Unix epoch in microseconds, but is not reliable to the microsecond, so we are using millisecond granularity. Process time is not available.
 
<syntaxhighlight lang="Quackery"> [ time ]'[ do
time swap - 1000 / ] is time: ( --> n )
 
time: [ 0 314159 times 1+ echo ]
cr cr
say "That took about " echo say " milliseconds."</syntaxhighlight>
 
{{out}}
 
<pre>314159
 
That took about 392 milliseconds.
</pre>
 
=={{header|R}}==
R has a built-in function, system.time, to calculate this.
<langsyntaxhighlight Rlang="r"># A task
foo <- function()
{
Line 672 ⟶ 2,440:
timer <- system.time(foo())
# Extract the processing time
timer["user.self"]</langsyntaxhighlight>
For a breakdown of processing time by function, there is Rprof.
<langsyntaxhighlight Rlang="r">Rprof()
foo()
Rprof(NULL)
summaryRprof()</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">
#lang racket
(define (fact n) (if (zero? n) 1 (* n (fact (sub1 n)))))
(time (fact 5000))
</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
Follows modern trend toward measuring wall-clock time, since CPU time is becoming rather ill-defined in the age of multicore, and doesn't reflect IO overhead in any case.
<syntaxhighlight lang="raku" line>my $start = now;
(^100000).pick(1000);
say now - $start;</syntaxhighlight>
{{out}}
<pre>0.02301709</pre>
 
=={{header|Raven}}==
<syntaxhighlight lang="raven">define doId use $x
$x dup * $x /
 
define doPower use $v, $p
$v $p pow
 
define doSort
group
20000 each choose
list sort reverse
 
define timeFunc use $fName
time as $t1
$fName "" prefer call
time as $t2
$fName $t2 $t1 -"%.4g secs for %s\n" print
 
"NULL" timeFunc
42 "doId" timeFunc
12 2 "doPower" timeFunc
"doSort" timeFunc</syntaxhighlight>
{{out}}
<pre>2.193e-05 secs for NULL
2.003e-05 secs for doId
4.601e-05 secs for doPower
3.028 secs for doSort</pre>
 
=={{header|Retro}}==
Retro has a '''time''' function returning the current time in seconds. This can be used to build a simple timing function:
 
<syntaxhighlight lang="retro">: .runtime ( a- ) time [ do time ] dip - "\n%d\n" puts ;
 
: test 20000 [ putn space ] iterd ;
&test .runtime</syntaxhighlight>
 
Finer measurements are not possible with the standard implementation.
 
=={{header|REXX}}==
===elapsed time version===
REXX doesn't have a language feature for obtaining true CPU time (except under
<br>IBM mainframes which have commands that can retrieve such times), but it does
<br>have a built-in function for elapsed time(s).
 
The main reason for the true CPU time omission is that REXX was developed under VM/CMS and
<br>there's a way to easily query the host (VM/CP) to indicate how much &nbsp; ''true'' &nbsp; CPU time was used by
<br>(normally) your own userID.&nbsp; The result can then be placed into a REXX variable (as an option).
<syntaxhighlight lang="rexx">/*REXX program displays the elapsed time for a REXX function (or subroutine). */
arg reps . /*obtain an optional argument from C.L.*/
if reps=='' then reps=100000 /*Not specified? No, then use default.*/
call time 'Reset' /*only the 1st character is examined. */
junk = silly(reps) /*invoke the SILLY function (below). */
/*───► CALL SILLY REPS also works.*/
 
/* The E is for elapsed time.*/
/* │ ─ */
/* ┌────◄───┘ */
/* │ */
/* ↓ */
say 'function SILLY took' format(time("E"),,2) 'seconds for' reps "iterations."
/* ↑ */
/* │ */
/* ┌────────►───────┘ */
/* │ */
/* The above 2 for the FORMAT function displays the time with*/
/* two decimal digits (rounded) past the decimal point). Using */
/* a 0 (zero) would round the time to whole seconds. */
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
silly: procedure /*chew up some CPU time doing some silly stuff.*/
do j=1 for arg(1) /*wash, apply, lather, rinse, repeat. ··· */
@.j=random() date() time() digits() fuzz() form() xrange() queued()
end /*j*/
return j-1</syntaxhighlight>
'''output''' &nbsp; when using a personal computer built in the 20th century:
<pre>
function SILLY took 3.54 seconds for 100000 iterations.
</pre>
'''output''' &nbsp; when using a personal computer built in the 21st century:
<pre>
function SILLY took 0.44 seconds for 100000 iterations.
</pre>
'''output''' &nbsp; when using an IBM mainframe with MVS/TSO:
<pre>
function SILLY took 0.69 seconds for 100000 iterations.
</pre>
 
===CPU time used version===
This version &nbsp; ''only'' &nbsp; works with Regina REXX as the &nbsp; '''J''' &nbsp; option &nbsp; (for the '''time''' BIF) &nbsp; is a Regina extension.
 
Since the &nbsp; '''silly''' &nbsp; function (by far) consumes the bulk of the CPU time of the REXX program, what is
<br>being measured is essentially the same as the wall clock time (duration) of the function execution; &nbsp; the
<br>overhead of the invocation is minimal compared to the overall time used.
<syntaxhighlight lang="rexx">/*REXX program displays the elapsed time for a REXX function (or subroutine). */
arg reps . /*obtain an optional argument from C.L.*/
if reps=='' then reps=100000 /*Not specified? No, then use default.*/
call time 'Reset' /*only the 1st character is examined. */
junk = silly(reps) /*invoke the SILLY function (below). */
/*───► CALL SILLY REPS also works.*/
 
/* The J is for the CPU time used */
/* │ by the REXX program since */
/* ┌───────┘ since the time was RESET. */
/* │ This is a Regina extension.*/
/* ↓ */
say 'function SILLY took' format(time("J"),,2) 'seconds for' reps "iterations."
/* ↑ */
/* │ */
/* ┌────────►───────┘ */
/* │ */
/* The above 2 for the FORMAT function displays the time with*/
/* two decimal digits (rounded) past the decimal point). Using */
/* a 0 (zero) would round the time to whole seconds. */
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
silly: procedure /*chew up some CPU time doing some silly stuff.*/
do j=1 for arg(1) /*wash, apply, lather, rinse, repeat. ··· */
@.j=random() date() time() digits() fuzz() form() xrange() queued()
end /*j*/
return j-1</syntaxhighlight>
'''output''' &nbsp; is essentially identical to the previous examples.
<br><br>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
beginTime = TimeList()[13]
for n = 1 to 10000000
n = n + 1
next
endTime = TimeList()[13]
elapsedTime = endTime - beginTime
see "Elapsed time = " + elapsedTime + nl
</syntaxhighlight>
 
=={{header|RPL}}==
In its first versions, RPL did not provide user access to system clock - but advanced users knew which system call can be made on their machine to get it. The following code works on a 1987-manufactured HP-28S, but can crash on older ones and will surely do on other machines. If using a newer model, replace <code>#11CAh SYSEVAL</code> by <code>TICKS</code>, which is a (safe) built-in instruction.
{| class="wikitable"
! RPL code
! Comment
|-
|
#11CAh SYSEVAL → tick
≪ EVAL
#11CAh SYSEVAL tick -
B→R 8192 / 1 FIX RND STD
≫ ≫ '<span style="color:blue">TEVAL</span>' STO
|
<span style="color:blue">TEVAL</span> ''( ≪function≫ -- execution_time )''
Store current system time
Execute function
Measure difference in CPU cycles
convert to seconds, round to one decimal place
|}
{{in}}
<pre>
≪ 1 1000 START NEXT ≫ TEVAL
</pre>
{{out}}
<pre>
1: 6.4
</pre>
Yes, more than 6 seconds to loop 1000 times is quite slow.
 
HP-49+ models have a built-in <code>TEVAL</code> command.
 
=={{header|Ruby}}==
Ruby's Benchmark module provides a way to generate nice reports (numbers are in seconds):
<langsyntaxhighlight lang="ruby">require 'benchmark'
 
Benchmark.bm(8) do |x|
x.report("nothing:") { }
x.report("sum:") { (1..1_000_000).inject(4) {|sum, x| sum + x} }
end</langsyntaxhighlight>
Output:
user system total real
nothing: 0.000000 0.000000 0.000000 ( 0.000014)
sum: 2.700000 0.400000 3.100000 ( 3.258348)
 
You can get the total time as a number for later processing like this:
<syntaxhighlight lang="ruby">Benchmark.measure { whatever }.total</syntaxhighlight>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">// 20210224 Rust programming solution
 
use rand::Rng;
use std::time::{Instant};
 
fn custom_function() {
let mut i = 0;
let mut rng = rand::thread_rng();
let n1: f32 = rng.gen();
 
while i < ( 1000000 + 1000000 * ( n1.log10() as i32 ) ) {
i = i + 1;
}
}
 
fn main() {
 
let start = Instant::now();
custom_function();
let duration = start.elapsed();
 
println!("Time elapsed in the custom_function() is : {:?}", duration);
}
</syntaxhighlight>
{{out}}
<pre>
Time elapsed in the custom_function() is : 39.615455ms
user system total real
nothing: 0.000000 0.000000 0.000000 ( 0.000014)
sum: 2.700000 0.400000 3.100000 ( 3.258348)
</pre>
 
=={{header|Scala}}==
You can get the total time as a number for later processing like this:
Define a <code>time</code> function that returns the elapsed time (in ms) to execute a block of code.
<lang ruby>Benchmark.measure { whatever }.total</lang>
<syntaxhighlight lang="scala">
def time(f: => Unit)={
val s = System.currentTimeMillis
f
System.currentTimeMillis - s
}
</syntaxhighlight>
Can be called with a code block:
<syntaxhighlight lang="scala">
println(time {
for(i <- 1 to 10000000) {}
})
</syntaxhighlight>
Or with a function:
<syntaxhighlight lang="scala">
def count(i:Int) = for(j <- 1 to i){}
println(time (count(10000000)))
</syntaxhighlight>
 
=={{header|Scheme}}==
<syntaxhighlight lang ="scheme">(time (some-function))</langsyntaxhighlight>
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "time.s7i";
include "duration.s7i";
const func integer: identity (in integer: x) is
return x;
const func integer: sum (in integer: num) is func
result
var integer: result is 0;
local
var integer: number is 0;
begin
result := num;
for number range 1 to 1000000 do
result +:= number;
end for;
end func;
const func duration: timeIt (ref func integer: aFunction) is func
result
var duration: result is duration.value;
local
var time: before is time.value;
begin
before := time(NOW);
ignore(aFunction);
result := time(NOW) - before;
end func;
const proc: main is func
begin
writeln("Identity(4) takes " <& timeIt(identity(4)));
writeln("Sum(4) takes " <& timeIt(sum(4)));
end func;</syntaxhighlight>
 
{{out}} of interpreted program:
Identity(4) takes 0-00-00 00:00:00.000163
Sum(4) takes 0-00-00 00:00:00.131823
 
{{out}} of compiled program (optimized with -O2):
Identity(4) takes 0-00-00 00:00:00.000072
Sum(4) takes 0-00-00 00:00:00.000857
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var benchmark = frequire('Benchmark')
 
func fac_rec(n) {
n == 0 ? 1 : (n * __FUNC__(n - 1))
}
 
func fac_iter(n) {
var prod = 1
n.times { |i|
prod *= i
}
prod
}
 
var result = benchmark.timethese(-3, Hash(
'fac_rec' => { fac_rec(20) },
'fac_iter' => { fac_iter(20) },
))
 
benchmark.cmpthese(result)</syntaxhighlight>
{{out}}
<pre>
Benchmark: running fac_iter, fac_rec for at least 3 CPU seconds...
fac_iter: 3 wallclock secs ( 3.23 usr + 0.00 sys = 3.23 CPU) @ 7331.89/s (n=23682)
fac_rec: 3 wallclock secs ( 3.19 usr + 0.00 sys = 3.19 CPU) @ 3551.72/s (n=11330)
Rate fac_rec fac_iter
fac_rec 3552/s -- -52%
fac_iter 7332/s 106% --
</pre>
 
=={{header|Slate}}==
<syntaxhighlight lang="slate">
[inform: 2000 factorial] timeToRun.
</syntaxhighlight>
 
=={{header|Smalltalk}}==
(Squeak/Pharo/VisualWorks/SmalltalkX)
<syntaxhighlight lang="smalltalk">Time millisecondsToRun: [
Transcript show: 2000 factorial
].</syntaxhighlight>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "time_function" )
@( description, "Write a program which uses a timer (with the least " )
@( description, "granularity available on your system) to time how " )
@( description, "long a function takes to execute." )
@( see_also, "http://rosettacode.org/wiki/Time_a_function" );
pragma annotate( author, "Ken O. Burtch" );
pragma license( unrestricted );
 
pragma restriction( no_external_commands );
 
procedure time_function is
 
procedure sample_function( num : in out integer ) is
begin
for i in 1..1000 loop
num := @+1;
end loop;
end sample_function;
 
start_time : calendar.time;
end_time : calendar.time;
seconds : duration;
 
procedure time_sample_function is
sample_param : integer := 4;
begin
start_time := calendar.clock;
sample_function( sample_param );
end_time := calendar.clock;
seconds := end_time - start_time;
end time_sample_function;
 
begin
time_sample_function;
put_line( "sum(4) takes:" & strings.image( seconds ) & " seconds." );
command_line.set_exit_status( 0 );
end time_function;</syntaxhighlight>
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">fun time_it (action, arg) = let
val timer = Timer.startCPUTimer ()
val _ = action arg
Line 707 ⟶ 2,838:
in
Time.+ (#usr times, #sys times)
end</langsyntaxhighlight>
 
===Example===
Line 723 ⟶ 2,854:
Sum(4) takes 0.220 seconds.
val it = () : unit
 
=={{header|Stata}}==
Stata can track up to 100 timers. See '''[http://www.stata.com/help.cgi?timer timer]''' in Stata help.
 
<syntaxhighlight lang="stata">program timer_test
timer clear 1
timer on 1
sleep `0'
timer off 1
timer list 1
end
 
. timer_test 1000
1: 1.01 / 1 = 1.0140</syntaxhighlight>
 
=={{header|Swift}}==
 
Using the 2-term ackermann function for demonstration.
 
<syntaxhighlight lang="swift">import Foundation
 
public struct TimeResult {
public var seconds: Double
public var nanoSeconds: Double
 
public var duration: Double { seconds + (nanoSeconds / 1e9) }
 
@usableFromInline
init(seconds: Double, nanoSeconds: Double) {
self.seconds = seconds
self.nanoSeconds = nanoSeconds
}
}
 
extension TimeResult: CustomStringConvertible {
public var description: String {
return "TimeResult(seconds: \(seconds); nanoSeconds: \(nanoSeconds); duration: \(duration)s)"
}
}
 
public struct ClockTimer {
@inlinable @inline(__always)
public static func time<T>(_ f: () throws -> T) rethrows -> (T, TimeResult) {
var tsi = timespec()
var tsf = timespec()
 
clock_gettime(CLOCK_MONOTONIC_RAW, &tsi)
let res = try f()
clock_gettime(CLOCK_MONOTONIC_RAW, &tsf)
 
let secondsElapsed = difftime(tsf.tv_sec, tsi.tv_sec)
let nanoSecondsElapsed = Double(tsf.tv_nsec - tsi.tv_nsec)
 
return (res, TimeResult(seconds: secondsElapsed, nanoSeconds: nanoSecondsElapsed))
}
}
 
func ackermann(m: Int, n: Int) -> Int {
switch (m, n) {
case (0, _):
return n + 1
case (_, 0):
return ackermann(m: m - 1, n: 1)
case (_, _):
return ackermann(m: m - 1, n: ackermann(m: m, n: n - 1))
}
}
 
let (n, t) = ClockTimer.time { ackermann(m: 3, n: 11) }
 
print("Took \(t.duration)s to calculate ackermann(m: 3, n: 11) = \(n)")
 
let (n2, t2) = ClockTimer.time { ackermann(m: 4, n: 1) }
 
print("Took \(t2.duration)s to calculate ackermann(m: 4, n: 1) = \(n2)")</syntaxhighlight>
 
{{out}}
 
<pre>Took 0.193593682s to calculate ackermann(m: 3, n: 11) = 16381
Took 3.103710995s to calculate ackermann(m: 4, n: 1) = 65533</pre>
 
=={{header|Tcl}}==
The Tcl <code>time</code> command returns the real time elapsed averaged over a number of iterations.
averaged over a number of iterations.
<lang tcl>proc sum_n {n} {
<syntaxhighlight lang="tcl">proc sum_n {n} {
for {set i 1; set sum 0.0} {$i <= $n} {incr i} {set sum [expr {$sum + $i}]}
return [expr {wide($sum)}]
Line 732 ⟶ 2,944:
 
puts [time {sum_n 1e6} 100]
puts [time {} 100]</langsyntaxhighlight>
{{out}}
Results in
<pre> 163551.0 microseconds per iteration
0.2 microseconds per iteration</pre>
 
=={{header|TorqueScript}}==
 
[[User:Greek2me|Greek2me]] 02:16, 19 June 2012 (UTC)
 
Returns average time elapsed from many iterations.
<syntaxhighlight lang="torquescript">
function benchmark(%times,%function,%a,%b,%c,%d,%e,%f,%g,%h,%i,%j,%k,%l,%m,%n,%o)
{
if(!isFunction(%function))
{
warn("BENCHMARKING RESULT FOR" SPC %function @ ":" NL "Function does not exist.");
return -1;
}
 
%start = getRealTime();
 
for(%i=0; %i < %times; %i++)
{
call(%function,%a,%b,%c,%d,%e,%f,%g,%h,%i,%j,%k,%l,%m,%n,%o);
}
 
%end = getRealTime();
 
%result = (%end-%start) / %times;
 
warn("BENCHMARKING RESULT FOR" SPC %function @ ":" NL %result);
 
return %result;
}
</syntaxhighlight>
 
{{out|Example}}
<syntaxhighlight lang="torquescript">
function exampleFunction(%var1,%var2)
{
//put stuff here
}
 
benchmark(500,"exampleFunction","blah","variables");
 
==> BENCHMARKING RESULT FOR exampleFunction:
==> 13.257
</syntaxhighlight>
 
=={{header|True BASIC}}==
{{trans|QBasic}}
<syntaxhighlight lang="qbasic">SUB cont (n)
LET sum = 0
FOR i = 1 TO n
LET sum = sum+1
NEXT i
END SUB
 
LET timestart = TIME
CALL cont (10000000)
LET timedone = TIME
 
!midnight check:
IF timedone < timestart THEN LET timedone = timedone+86400
LET timeelapsed = (timedone-timestart)*1000
PRINT timeelapsed; "miliseconds."
END</syntaxhighlight>
 
=={{header|TUSCRIPT}}==
<syntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
SECTION test
LOOP n=1,999999
rest=MOD (n,1000)
IF (rest==0) Print n
ENDLOOP
ENDSECTION
time_beg=TIME ()
DO test
time_end=TIME ()
interval=TIME_INTERVAL (seconds,time_beg,time_end)
PRINT "'test' start at ",time_beg
PRINT "'test' ends at ",time_end
PRINT "'test' takes ",interval," seconds"
</syntaxhighlight>
{{out}}
'test' start at 2011-01-15 14:38:22
'test' ends at 2011-01-15 14:38:31
'test' takes 9 seconds
 
=={{header|UNIX Shell}}==
<langsyntaxhighlight lang="bash">$ time sleep 1</syntaxhighlight>
 
real 0m1.074s
user 0m0.001s</lang>
sys 0m0.006s
 
=={{header|VBA}}==
{{omit from|Batch File|No way of programmatically retrieving the current or elapsed time, therefore impossible. The only way are human-readable formats which can't be parsed accurately.}}
<syntaxhighlight lang="vb">Public Declare Function GetTickCount Lib "kernel32.dll" () As Long
Private Function identity(x As Long) As Long
For j = 0 To 1000
identity = x
Next j
End Function
Private Function sum(ByVal num As Long) As Long
Dim t As Long
For j = 0 To 1000
t = num
For i = 0 To 10000
t = t + i
Next i
Next j
sum = t
End Function
Private Sub time_it()
Dim start_time As Long, finis_time As Long
start_time = GetTickCount
identity 1
finis_time = GetTickCount
Debug.Print "1000 times Identity(1) takes "; (finis_time - start_time); " milliseconds"
start_time = GetTickCount
sum 1
finis_time = GetTickCount
Debug.Print "1000 times Sum(1) takes "; (finis_time - start_time); " milliseconds"
End Sub</syntaxhighlight>{{out}}<pre>1000 times Identity(1) takes 0 seconds
1000 times Sum(1) takes 296 seconds</pre>
 
=={{header|Wart}}==
<syntaxhighlight lang="python">time 1+1
30000/1000000 # in microseconds
=> 2</syntaxhighlight>
 
=={{header|Wren}}==
{{libheader|Wren-check}}
The only way Wren currently has to time a function (encapsulated in the ''Benchmark.run'' method) is to measure the System time before and after the function is called. We therefore use that approach, averaging over say 100 runs, having first shut down as many other processes as we can.
<syntaxhighlight lang="wren">import "./check" for Benchmark
 
Benchmark.run("a function", 100, true) {
for (i in 0..1e7) {}
}</syntaxhighlight>
 
{{out}}
<pre>
Running 'a function' over 100 iteration(s):
-------------------------------------------
Best 191.172 ms
Mean 201.942 ms
Worst 208.049 ms
</pre>
 
=={{header|XPL0}}==
This works fine under pure DOS but has problems under Windows.
Windows can execute other processes, although it could be argued
that this should be included as part of the total time
to accomplish the task at hand.
DOS does go off to service a timer interrupt,
but it's usually very fast, although beware of TSRs
that hook this interrupt handler.
 
There's a more serious problem with the GetTime intrinsic
under Windows XP.
GetTime provides microsecond resolution by combining
the BIOS timer interrupt count at location 046C
with the count in the 8254 chip (or its equivalent).
Unfortunately, Windows virtualizes the 8254
and thus the two values can be out of sync.
 
<syntaxhighlight lang="xpl0">include c:\cxpl\codes;
int T0, T1, I;
[T0:= GetTime;
for I:= 1, 1_000_000 do [];
T1:= GetTime;
IntOut(0, T1-T0); Text(0, " microseconds^M^J");
]</syntaxhighlight>
 
{{out|Example output}} for a Duron 850 running DOS 5.0:
<pre>
2354 microseconds
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">sub count(n)
local i
for i = 1 to n
next i
end sub
 
count(1000000)
 
print peek("millisrunning"), " milliseconds"
 
t0 = peek("millisrunning")
count(10000000)
print peek("millisrunning")-t0, " milliseconds"</syntaxhighlight>
 
=={{header|zkl}}==
In order to be as OS independent as possible, only system time is available.
<syntaxhighlight lang="zkl">t:=Time.Clock.time; Atomic.sleep(3); (Time.Clock.time - t).println();</syntaxhighlight>
{{out}}
<pre>3</pre>
 
=={{header|ZX Spectrum Basic}}==
The ZX Spectrum has very little in the way of timing functionality; its best clock is the three-byte FRAMES variable, which starts at zero when the system is turned on and updates every time the ULA refreshes the screen, giving a granularity of one fiftieth of a second. As the system cannot multitask, a difference between the start and end time is as good as we can get; certain actions, most notably operating the system BEEPer, temporarily stop the counter.
 
<syntaxhighlight lang="zxbasic">
1 DEF FN t()=(PEEK 23672+256*PEEK 23673+65536*PEEK 23674)/50
10 LET time=FN t()
20 PRINT ASN 0.5
30 PRINT FN t()-time</syntaxhighlight>
{{out}}
<pre>0.52359878
0.22
 
0 OK, 30:1</pre>
9,479

edits