Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Time-a-function/00-META.yaml
Normal file
5
Task/Time-a-function/00-META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- Date and time
|
||||
from: http://rosettacode.org/wiki/Time_a_function
|
||||
note: Programming environment operations
|
||||
12
Task/Time-a-function/00-TASK.txt
Normal file
12
Task/Time-a-function/00-TASK.txt
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
;Task:
|
||||
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>
|
||||
|
||||
12
Task/Time-a-function/11l/time-a-function.11l
Normal file
12
Task/Time-a-function/11l/time-a-function.11l
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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)))
|
||||
105
Task/Time-a-function/8051-Assembly/time-a-function.8051
Normal file
105
Task/Time-a-function/8051-Assembly/time-a-function.8051
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
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
|
||||
1
Task/Time-a-function/ACL2/time-a-function.acl2
Normal file
1
Task/Time-a-function/ACL2/time-a-function.acl2
Normal file
|
|
@ -0,0 +1 @@
|
|||
(time$ (nthcdr 9999999 (take 10000000 nil)))
|
||||
229
Task/Time-a-function/ARM-Assembly/time-a-function.arm
Normal file
229
Task/Time-a-function/ARM-Assembly/time-a-function.arm
Normal file
|
|
@ -0,0 +1,229 @@
|
|||
/* 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
|
||||
44
Task/Time-a-function/Action-/time-a-function.action
Normal file
44
Task/Time-a-function/Action-/time-a-function.action
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
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
|
||||
31
Task/Time-a-function/Ada/time-a-function.ada
Normal file
31
Task/Time-a-function/Ada/time-a-function.ada
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
with Ada.Calendar; use Ada.Calendar;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Query_Performance is
|
||||
type Proc_Access is access procedure(X : in out Integer);
|
||||
function Time_It(Action : Proc_Access; Arg : Integer) return Duration is
|
||||
Start_Time : Time := Clock;
|
||||
Finis_Time : Time;
|
||||
Func_Arg : Integer := Arg;
|
||||
begin
|
||||
Action(Func_Arg);
|
||||
Finis_Time := Clock;
|
||||
return Finis_Time - Start_Time;
|
||||
end Time_It;
|
||||
procedure Identity(X : in out Integer) is
|
||||
begin
|
||||
X := X;
|
||||
end Identity;
|
||||
procedure Sum (Num : in out Integer) is
|
||||
begin
|
||||
for I in 1..1000 loop
|
||||
Num := Num + I;
|
||||
end loop;
|
||||
end Sum;
|
||||
Id_Access : Proc_Access := Identity'access;
|
||||
Sum_Access : Proc_Access := Sum'access;
|
||||
|
||||
begin
|
||||
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;
|
||||
50
Task/Time-a-function/Aime/time-a-function.aime
Normal file
50
Task/Time-a-function/Aime/time-a-function.aime
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
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;
|
||||
}
|
||||
5
Task/Time-a-function/Arturo/time-a-function.arturo
Normal file
5
Task/Time-a-function/Arturo/time-a-function.arturo
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
benchmark [
|
||||
print "starting function"
|
||||
pause 2000
|
||||
print "function ended"
|
||||
]
|
||||
15
Task/Time-a-function/AutoHotkey/time-a-function-1.ahk
Normal file
15
Task/Time-a-function/AutoHotkey/time-a-function-1.ahk
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
MsgBox % time("fx")
|
||||
Return
|
||||
|
||||
fx()
|
||||
{
|
||||
Sleep, 1000
|
||||
}
|
||||
|
||||
time(function, parameter=0)
|
||||
{
|
||||
SetBatchLines -1 ; don't sleep for other green threads
|
||||
StartTime := A_TickCount
|
||||
%function%(parameter)
|
||||
Return ElapsedTime := A_TickCount - StartTime . " milliseconds"
|
||||
}
|
||||
16
Task/Time-a-function/AutoHotkey/time-a-function-2.ahk
Normal file
16
Task/Time-a-function/AutoHotkey/time-a-function-2.ahk
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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
|
||||
}
|
||||
9
Task/Time-a-function/BASIC/time-a-function.basic
Normal file
9
Task/Time-a-function/BASIC/time-a-function.basic
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
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
|
||||
14
Task/Time-a-function/BASIC256/time-a-function.basic
Normal file
14
Task/Time-a-function/BASIC256/time-a-function.basic
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
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
|
||||
3
Task/Time-a-function/BBC-BASIC/time-a-function.basic
Normal file
3
Task/Time-a-function/BBC-BASIC/time-a-function.basic
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
start%=TIME:REM centi-second timer
|
||||
REM perform processing
|
||||
lapsed%=TIME-start%
|
||||
1
Task/Time-a-function/BQN/time-a-function-1.bqn
Normal file
1
Task/Time-a-function/BQN/time-a-function-1.bqn
Normal file
|
|
@ -0,0 +1 @@
|
|||
F •_timed v
|
||||
4
Task/Time-a-function/BQN/time-a-function-2.bqn
Normal file
4
Task/Time-a-function/BQN/time-a-function-2.bqn
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
{0:1;𝕩×𝕊𝕩-1}•_timed 100
|
||||
8.437800000000001e¯05
|
||||
{0:1;𝕩×𝕊𝕩-1}•_timed 1000
|
||||
0.000299545
|
||||
9
Task/Time-a-function/BaCon/time-a-function.bacon
Normal file
9
Task/Time-a-function/BaCon/time-a-function.bacon
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
' Time a function
|
||||
SUB timed()
|
||||
SLEEP 7000
|
||||
END SUB
|
||||
|
||||
st = TIMER
|
||||
timed()
|
||||
et = TIMER
|
||||
PRINT st, ", ", et
|
||||
22
Task/Time-a-function/Batch-File/time-a-function.bat
Normal file
22
Task/Time-a-function/Batch-File/time-a-function.bat
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
@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
|
||||
14
Task/Time-a-function/Bracmat/time-a-function.bracmat
Normal file
14
Task/Time-a-function/Bracmat/time-a-function.bracmat
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
( ( 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)
|
||||
)
|
||||
23
Task/Time-a-function/C++/time-a-function-1.cpp
Normal file
23
Task/Time-a-function/C++/time-a-function-1.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#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;
|
||||
}
|
||||
40
Task/Time-a-function/C++/time-a-function-2.cpp
Normal file
40
Task/Time-a-function/C++/time-a-function-2.cpp
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
// 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;
|
||||
}
|
||||
22
Task/Time-a-function/C-sharp/time-a-function-1.cs
Normal file
22
Task/Time-a-function/C-sharp/time-a-function-1.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Diagnostics;
|
||||
|
||||
class Program {
|
||||
static void Main(string[] args) {
|
||||
Stopwatch sw = new Stopwatch();
|
||||
|
||||
sw.Start();
|
||||
DoSomething();
|
||||
sw.Stop();
|
||||
|
||||
Console.WriteLine("DoSomething() took {0}ms.", sw.Elapsed.TotalMilliseconds);
|
||||
}
|
||||
|
||||
static void DoSomething() {
|
||||
Thread.Sleep(1000);
|
||||
|
||||
Enumerable.Range(1, 10000).Where(x => x % 2 == 0).Sum(); // Sum even numers from 1 to 10000
|
||||
}
|
||||
}
|
||||
21
Task/Time-a-function/C-sharp/time-a-function-2.cs
Normal file
21
Task/Time-a-function/C-sharp/time-a-function-2.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
class Program {
|
||||
static void Main(string[] args) {
|
||||
DateTime start, end;
|
||||
|
||||
start = DateTime.Now;
|
||||
DoSomething();
|
||||
end = DateTime.Now;
|
||||
|
||||
Console.WriteLine("DoSomething() took " + (end - start).TotalMilliseconds + "ms");
|
||||
}
|
||||
|
||||
static void DoSomething() {
|
||||
Thread.Sleep(1000);
|
||||
|
||||
Enumerable.Range(1, 10000).Where(x => x % 2 == 0).Sum(); // Sum even numers from 1 to 10000
|
||||
}
|
||||
}
|
||||
39
Task/Time-a-function/C/time-a-function.c
Normal file
39
Task/Time-a-function/C/time-a-function.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int identity(int x) { return x; }
|
||||
|
||||
int sum(int s)
|
||||
{
|
||||
int i;
|
||||
for(i=0; i < 1000000; i++) s += i;
|
||||
return s;
|
||||
}
|
||||
|
||||
#ifdef CLOCK_PROCESS_CPUTIME_ID
|
||||
/* 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)
|
||||
{
|
||||
struct timespec tsi, tsf;
|
||||
|
||||
clock_gettime(CLOCKTYPE, &tsi);
|
||||
action(arg);
|
||||
clock_gettime(CLOCKTYPE, &tsf);
|
||||
|
||||
double elaps_s = difftime(tsf.tv_sec, tsi.tv_sec);
|
||||
long elaps_ns = tsf.tv_nsec - tsi.tv_nsec;
|
||||
return elaps_s + ((double)elaps_ns) / 1.0e9;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("identity (4) takes %lf s\n", time_it(identity, 4));
|
||||
printf("sum (4) takes %lf s\n", time_it(sum, 4));
|
||||
return 0;
|
||||
}
|
||||
7
Task/Time-a-function/Clojure/time-a-function.clj
Normal file
7
Task/Time-a-function/Clojure/time-a-function.clj
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(defn fib []
|
||||
(map first
|
||||
(iterate
|
||||
(fn [[a b]] [b (+ a b)])
|
||||
[0 1])))
|
||||
|
||||
(time (take 100 (fib)))
|
||||
8
Task/Time-a-function/Common-Lisp/time-a-function-1.lisp
Normal file
8
Task/Time-a-function/Common-Lisp/time-a-function-1.lisp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
CL-USER> (time (reduce #'+ (make-list 100000 :initial-element 1)))
|
||||
Evaluation took:
|
||||
0.151 seconds of real time
|
||||
0.019035 seconds of user run time
|
||||
0.01807 seconds of system run time
|
||||
0 calls to %EVAL
|
||||
0 page faults and
|
||||
2,400,256 bytes consed.
|
||||
10
Task/Time-a-function/Common-Lisp/time-a-function-2.lisp
Normal file
10
Task/Time-a-function/Common-Lisp/time-a-function-2.lisp
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(defun timings (function)
|
||||
(let ((real-base (get-internal-real-time))
|
||||
(run-base (get-internal-run-time)))
|
||||
(funcall function)
|
||||
(values (/ (- (get-internal-real-time) real-base) internal-time-units-per-second)
|
||||
(/ (- (get-internal-run-time) run-base) internal-time-units-per-second))))
|
||||
|
||||
CL-USER> (timings (lambda () (reduce #'+ (make-list 100000 :initial-element 1))))
|
||||
17/500
|
||||
7/250
|
||||
24
Task/Time-a-function/D/time-a-function-1.d
Normal file
24
Task/Time-a-function/D/time-a-function-1.d
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import std.stdio, std.datetime;
|
||||
|
||||
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));
|
||||
}
|
||||
27
Task/Time-a-function/D/time-a-function-2.d
Normal file
27
Task/Time-a-function/D/time-a-function-2.d
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import tango.io.Stdout;
|
||||
import tango.time.Clock;
|
||||
|
||||
int identity (int x)
|
||||
{
|
||||
return x;
|
||||
}
|
||||
|
||||
int sum (int num)
|
||||
{
|
||||
for (int i = 0; i < 1000000; i++)
|
||||
num += i;
|
||||
return num;
|
||||
}
|
||||
|
||||
double timeIt(int function(int) func, int arg)
|
||||
{
|
||||
long before = Clock.now.ticks;
|
||||
func(arg);
|
||||
return (Clock.now.ticks - before) / cast(double)TimeSpan.TicksPerSecond;
|
||||
}
|
||||
|
||||
void main ()
|
||||
{
|
||||
Stdout.format("Identity(4) takes {:f6} seconds",timeIt(&identity,4)).newline;
|
||||
Stdout.format("Sum(4) takes {:f6} seconds",timeIt(&sum,4)).newline;
|
||||
}
|
||||
58
Task/Time-a-function/Delphi/time-a-function.delphi
Normal file
58
Task/Time-a-function/Delphi/time-a-function.delphi
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
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;
|
||||
17
Task/Time-a-function/E/time-a-function.e
Normal file
17
Task/Time-a-function/E/time-a-function.e
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
def countTo(x) {
|
||||
println("Counting...")
|
||||
for _ in 1..x {}
|
||||
println("Done!")
|
||||
}
|
||||
|
||||
def MX := <unsafe:java.lang.management.makeManagementFactory>
|
||||
def threadMX := MX.getThreadMXBean()
|
||||
require(threadMX.isCurrentThreadCpuTimeSupported())
|
||||
threadMX.setThreadCpuTimeEnabled(true)
|
||||
|
||||
for count in [10000, 100000] {
|
||||
def start := threadMX.getCurrentThreadCpuTime()
|
||||
countTo(count)
|
||||
def finish := threadMX.getCurrentThreadCpuTime()
|
||||
println(`Counting to $count takes ${(finish-start)//1000000}ms`)
|
||||
}
|
||||
26
Task/Time-a-function/EMal/time-a-function.emal
Normal file
26
Task/Time-a-function/EMal/time-a-function.emal
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
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")
|
||||
23
Task/Time-a-function/Elena/time-a-function.elena
Normal file
23
Task/Time-a-function/Elena/time-a-function.elena
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
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)
|
||||
}
|
||||
2
Task/Time-a-function/Elixir/time-a-function-1.elixir
Normal file
2
Task/Time-a-function/Elixir/time-a-function-1.elixir
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
iex(10)> :timer.tc(fn -> Enum.each(1..100000, fn x -> x*x end) end)
|
||||
{236000, :ok}
|
||||
2
Task/Time-a-function/Elixir/time-a-function-2.elixir
Normal file
2
Task/Time-a-function/Elixir/time-a-function-2.elixir
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
iex(11)> :timer.tc(fn x -> Enum.each(1..x, fn y -> y*y end) end, [1000000])
|
||||
{2300000, :ok}
|
||||
5
Task/Time-a-function/Elixir/time-a-function-3.elixir
Normal file
5
Task/Time-a-function/Elixir/time-a-function-3.elixir
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
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, ...]}
|
||||
5
Task/Time-a-function/Erlang/time-a-function-1.erl
Normal file
5
Task/Time-a-function/Erlang/time-a-function-1.erl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
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.
|
||||
2
Task/Time-a-function/Erlang/time-a-function-2.erl
Normal file
2
Task/Time-a-function/Erlang/time-a-function-2.erl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
9> timer:tc(fun (X) -> lists:foreach(fun(Y) -> Y*Y end, lists:seq(1,X)) end, [1000000]).
|
||||
{2293844,ok}
|
||||
4
Task/Time-a-function/Erlang/time-a-function-3.erl
Normal file
4
Task/Time-a-function/Erlang/time-a-function-3.erl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
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|...]}
|
||||
5
Task/Time-a-function/Euphoria/time-a-function.euphoria
Normal file
5
Task/Time-a-function/Euphoria/time-a-function.euphoria
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
atom t
|
||||
t = time()
|
||||
some_procedure()
|
||||
t = time() - t
|
||||
printf(1,"Elapsed %f seconds.\n",t)
|
||||
8
Task/Time-a-function/F-Sharp/time-a-function.fs
Normal file
8
Task/Time-a-function/F-Sharp/time-a-function.fs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
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
|
||||
3
Task/Time-a-function/Factor/time-a-function.factor
Normal file
3
Task/Time-a-function/Factor/time-a-function.factor
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
USING: kernel sequences tools.time ;
|
||||
|
||||
[ 10000 <iota> sum drop ] time
|
||||
6
Task/Time-a-function/Forth/time-a-function.fth
Normal file
6
Task/Time-a-function/Forth/time-a-function.fth
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
: time: ( "word" -- )
|
||||
utime 2>R ' EXECUTE
|
||||
utime 2R> D-
|
||||
<# # # # # # # [CHAR] . HOLD #S #> TYPE ." seconds" ;
|
||||
|
||||
1000 time: MS \ 1.000081 seconds ok
|
||||
18
Task/Time-a-function/Fortran/time-a-function.f
Normal file
18
Task/Time-a-function/Fortran/time-a-function.f
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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
|
||||
19
Task/Time-a-function/FreeBASIC/time-a-function.basic
Normal file
19
Task/Time-a-function/FreeBASIC/time-a-function.basic
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
' 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
|
||||
2
Task/Time-a-function/GAP/time-a-function.gap
Normal file
2
Task/Time-a-function/GAP/time-a-function.gap
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# Return the time passed in last function
|
||||
time;
|
||||
9
Task/Time-a-function/Go/time-a-function-1.go
Normal file
9
Task/Time-a-function/Go/time-a-function-1.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package empty
|
||||
|
||||
func Empty() {}
|
||||
|
||||
func Count() {
|
||||
// count to a million
|
||||
for i := 0; i < 1e6; i++ {
|
||||
}
|
||||
}
|
||||
15
Task/Time-a-function/Go/time-a-function-2.go
Normal file
15
Task/Time-a-function/Go/time-a-function-2.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
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()
|
||||
}
|
||||
}
|
||||
31
Task/Time-a-function/Go/time-a-function-3.go
Normal file
31
Task/Time-a-function/Go/time-a-function-3.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
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))
|
||||
}
|
||||
25
Task/Time-a-function/Go/time-a-function-4.go
Normal file
25
Task/Time-a-function/Go/time-a-function-4.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
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()
|
||||
}
|
||||
12
Task/Time-a-function/Groovy/time-a-function-1.groovy
Normal file
12
Task/Time-a-function/Groovy/time-a-function-1.groovy
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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
|
||||
}
|
||||
5
Task/Time-a-function/Groovy/time-a-function-2.groovy
Normal file
5
Task/Time-a-function/Groovy/time-a-function-2.groovy
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def clockRealTime = { Closure c ->
|
||||
def start = System.currentTimeMillis()
|
||||
c.call()
|
||||
System.currentTimeMillis() - start
|
||||
}
|
||||
11
Task/Time-a-function/Groovy/time-a-function-3.groovy
Normal file
11
Task/Time-a-function/Groovy/time-a-function-3.groovy
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
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}"
|
||||
}
|
||||
}
|
||||
5
Task/Time-a-function/Halon/time-a-function.halon
Normal file
5
Task/Time-a-function/Halon/time-a-function.halon
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
$t = uptime();
|
||||
|
||||
sleep(1);
|
||||
|
||||
echo uptime() - $t;
|
||||
13
Task/Time-a-function/Haskell/time-a-function.hs
Normal file
13
Task/Time-a-function/Haskell/time-a-function.hs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
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
|
||||
startTime <- getCPUTime
|
||||
action arg
|
||||
finishTime <- getCPUTime
|
||||
return $ fromIntegral (finishTime - startTime) / 1000000000000
|
||||
|
||||
-- Version for use with evaluating regular non-monadic functions
|
||||
timeIt_ :: (Fractional c) => (a -> b) -> a -> IO c
|
||||
timeIt_ f = timeIt ((`seq` return ()) . f)
|
||||
5
Task/Time-a-function/HicEst/time-a-function.hicest
Normal file
5
Task/Time-a-function/HicEst/time-a-function.hicest
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
t_start = TIME() ! returns seconds since midnight
|
||||
SYSTEM(WAIT = 1234) ! wait 1234 milliseconds
|
||||
t_end = TIME()
|
||||
|
||||
WRITE(StatusBar) t_end - t_start, " seconds"
|
||||
35
Task/Time-a-function/Icon/time-a-function-1.icon
Normal file
35
Task/Time-a-function/Icon/time-a-function-1.icon
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
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 := [], -®ions) # . 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 := ®ions ) 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
|
||||
6
Task/Time-a-function/Icon/time-a-function-2.icon
Normal file
6
Task/Time-a-function/Icon/time-a-function-2.icon
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
procedure main()
|
||||
timef(perfectnumbers)
|
||||
end
|
||||
|
||||
procedure perfectnumbers()
|
||||
...
|
||||
5
Task/Time-a-function/Ioke/time-a-function.ioke
Normal file
5
Task/Time-a-function/Ioke/time-a-function.ioke
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
use("benchmark")
|
||||
|
||||
func = method((1..50000) reduce(+))
|
||||
|
||||
Benchmark report(1, 1, func)
|
||||
6
Task/Time-a-function/J/time-a-function.j
Normal file
6
Task/Time-a-function/J/time-a-function.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(6!:2 , 7!:2) '|: 50 50 50 $ i. 50^3' (6!:2,7!:2) '|: 50 50 50 $ i. 50^3'
|
||||
0.0014169 2.09875e6
|
||||
timespacex '|: 50 50 50 $ i. 50^3'
|
||||
0.0014129 2.09875e6
|
||||
timex '|: 50 50 50 $ i. 50^3'
|
||||
0.0015032
|
||||
11
Task/Time-a-function/Janet/time-a-function.janet
Normal file
11
Task/Time-a-function/Janet/time-a-function.janet
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(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))
|
||||
3
Task/Time-a-function/Java/time-a-function-1.java
Normal file
3
Task/Time-a-function/Java/time-a-function-1.java
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
long start = System.currentTimeMillis();
|
||||
/* code you want to time, here */
|
||||
long duration = System.currentTimeMillis() - start;
|
||||
27
Task/Time-a-function/Java/time-a-function-2.java
Normal file
27
Task/Time-a-function/Java/time-a-function-2.java
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.ThreadMXBean;
|
||||
|
||||
public class TimeIt {
|
||||
public static void main(String[] args) {
|
||||
final ThreadMXBean threadMX = ManagementFactory.getThreadMXBean();
|
||||
assert threadMX.isCurrentThreadCpuTimeSupported();
|
||||
threadMX.setThreadCpuTimeEnabled(true);
|
||||
|
||||
long start, end;
|
||||
start = threadMX.getCurrentThreadCpuTime();
|
||||
countTo(100000000);
|
||||
end = threadMX.getCurrentThreadCpuTime();
|
||||
System.out.println("Counting to 100000000 takes "+(end-start)/1000000+"ms");
|
||||
start = threadMX.getCurrentThreadCpuTime();
|
||||
countTo(1000000000L);
|
||||
end = threadMX.getCurrentThreadCpuTime();
|
||||
System.out.println("Counting to 1000000000 takes "+(end-start)/1000000+"ms");
|
||||
|
||||
}
|
||||
|
||||
public static void countTo(long x){
|
||||
System.out.println("Counting...");
|
||||
for(long i=0;i<x;i++);
|
||||
System.out.println("Done!");
|
||||
}
|
||||
}
|
||||
12
Task/Time-a-function/Java/time-a-function-3.java
Normal file
12
Task/Time-a-function/Java/time-a-function-3.java
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
public static void main(String[] args){
|
||||
long start, end;
|
||||
start = System.currentTimeMillis();
|
||||
countTo(100000000);
|
||||
end = System.currentTimeMillis();
|
||||
System.out.println("Counting to 100000000 takes "+(end-start)+"ms");
|
||||
start = System.currentTimeMillis();
|
||||
countTo(1000000000L);
|
||||
end = System.currentTimeMillis();
|
||||
System.out.println("Counting to 1000000000 takes "+(end-start)+"ms");
|
||||
|
||||
}
|
||||
12
Task/Time-a-function/JavaScript/time-a-function.js
Normal file
12
Task/Time-a-function/JavaScript/time-a-function.js
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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
|
||||
3
Task/Time-a-function/Joy/time-a-function.joy
Normal file
3
Task/Time-a-function/Joy/time-a-function.joy
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
clock
|
||||
1 1023 [dup +] times pop
|
||||
clock swap -.
|
||||
13
Task/Time-a-function/Julia/time-a-function.julia
Normal file
13
Task/Time-a-function/Julia/time-a-function.julia
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# 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)
|
||||
24
Task/Time-a-function/Kotlin/time-a-function.kotlin
Normal file
24
Task/Time-a-function/Kotlin/time-a-function.kotlin
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// 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")
|
||||
}
|
||||
}
|
||||
5
Task/Time-a-function/Lasso/time-a-function.lasso
Normal file
5
Task/Time-a-function/Lasso/time-a-function.lasso
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
local(start = micros)
|
||||
loop(100000) => {
|
||||
'nothing is outout because no autocollect'
|
||||
}
|
||||
'time for 100,000 loop repititions: '+(micros - #start)+' microseconds'
|
||||
5
Task/Time-a-function/Lingo/time-a-function-1.lingo
Normal file
5
Task/Time-a-function/Lingo/time-a-function-1.lingo
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
on testFunc ()
|
||||
repeat with i = 1 to 1000000
|
||||
x = sqrt(log(i))
|
||||
end repeat
|
||||
end
|
||||
5
Task/Time-a-function/Lingo/time-a-function-2.lingo
Normal file
5
Task/Time-a-function/Lingo/time-a-function-2.lingo
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
ms = _system.milliseconds
|
||||
testFunc()
|
||||
ms = _system.milliseconds - ms
|
||||
put "Execution time in ms:" && ms
|
||||
-- "Execution time in ms: 983"
|
||||
10
Task/Time-a-function/Logo/time-a-function.logo
Normal file
10
Task/Time-a-function/Logo/time-a-function.logo
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
to time
|
||||
output first first shell "|date +%s|
|
||||
end
|
||||
to elapsed :block
|
||||
localmake "start time
|
||||
run :block
|
||||
(print time - :start [seconds elapsed])
|
||||
end
|
||||
|
||||
elapsed [wait 300] ; 5 seconds elapsed
|
||||
12
Task/Time-a-function/Lua/time-a-function.lua
Normal file
12
Task/Time-a-function/Lua/time-a-function.lua
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
function Test_Function()
|
||||
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 ) )
|
||||
43
Task/Time-a-function/M2000-Interpreter/time-a-function.m2000
Normal file
43
Task/Time-a-function/M2000-Interpreter/time-a-function.m2000
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
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
|
||||
1
Task/Time-a-function/Maple/time-a-function-1.maple
Normal file
1
Task/Time-a-function/Maple/time-a-function-1.maple
Normal file
|
|
@ -0,0 +1 @@
|
|||
CodeTools:-Usage(ifactor(32!+1), output = realtime, quiet);
|
||||
1
Task/Time-a-function/Maple/time-a-function-2.maple
Normal file
1
Task/Time-a-function/Maple/time-a-function-2.maple
Normal file
|
|
@ -0,0 +1 @@
|
|||
CodeTools:-Usage(ifactor(32!+1), output = cputime, quiet);
|
||||
1
Task/Time-a-function/Mathematica/time-a-function-1.math
Normal file
1
Task/Time-a-function/Mathematica/time-a-function-1.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
AbsoluteTiming[x];
|
||||
1
Task/Time-a-function/Mathematica/time-a-function-2.math
Normal file
1
Task/Time-a-function/Mathematica/time-a-function-2.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
AbsoluteTiming[N[Sqrt[3], 10^6]]
|
||||
1
Task/Time-a-function/Mathematica/time-a-function-3.math
Normal file
1
Task/Time-a-function/Mathematica/time-a-function-3.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
{0.000657, 1.7320508075688772935274463......}
|
||||
16
Task/Time-a-function/Maxima/time-a-function.maxima
Normal file
16
Task/Time-a-function/Maxima/time-a-function.maxima
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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
|
||||
5
Task/Time-a-function/MiniScript/time-a-function.mini
Normal file
5
Task/Time-a-function/MiniScript/time-a-function.mini
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
start = time
|
||||
for i in range(1,100000)
|
||||
end for
|
||||
duration = time - start
|
||||
print "Process took " + duration + " seconds"
|
||||
13
Task/Time-a-function/Nim/time-a-function.nim
Normal file
13
Task/Time-a-function/Nim/time-a-function.nim
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
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"
|
||||
5
Task/Time-a-function/OCaml/time-a-function.ocaml
Normal file
5
Task/Time-a-function/OCaml/time-a-function.ocaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
let time_it action arg =
|
||||
let start_time = Sys.time () in
|
||||
ignore (action arg);
|
||||
let finish_time = Sys.time () in
|
||||
finish_time -. start_time
|
||||
19
Task/Time-a-function/Oz/time-a-function.oz
Normal file
19
Task/Time-a-function/Oz/time-a-function.oz
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
declare
|
||||
%% returns milliseconds
|
||||
fun {TimeIt Proc}
|
||||
Before = {Now}
|
||||
in
|
||||
{Proc}
|
||||
{Now} - Before
|
||||
end
|
||||
|
||||
fun {Now}
|
||||
{Property.get 'time.total'}
|
||||
end
|
||||
in
|
||||
{Show
|
||||
{TimeIt
|
||||
proc {$}
|
||||
{FoldL {List.number 1 1000000 1} Number.'+' 4 _}
|
||||
end}
|
||||
}
|
||||
4
Task/Time-a-function/PARI-GP/time-a-function-1.parigp
Normal file
4
Task/Time-a-function/PARI-GP/time-a-function-1.parigp
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
time(foo)={
|
||||
foo();
|
||||
gettime();
|
||||
}
|
||||
5
Task/Time-a-function/PARI-GP/time-a-function-2.parigp
Normal file
5
Task/Time-a-function/PARI-GP/time-a-function-2.parigp
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
time(foo)={
|
||||
my(start=getabstime());
|
||||
foo();
|
||||
getabstime()-start;
|
||||
}
|
||||
15
Task/Time-a-function/PL-I/time-a-function.pli
Normal file
15
Task/Time-a-function/PL-I/time-a-function.pli
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
declare (start_time, finish_time) float (18);
|
||||
|
||||
start_time = secs();
|
||||
|
||||
do i = 1 to 10000000;
|
||||
/* something to be repeated goes here. */
|
||||
end;
|
||||
finish_time = secs();
|
||||
|
||||
put skip edit ('elapsed time=', finish_time - start_time, ' seconds')
|
||||
(A, F(10,3), A);
|
||||
/* gives the result to thousandths of a second. */
|
||||
|
||||
/* Note: using the SECS function takes into account the clock */
|
||||
/* going past midnight. */
|
||||
18
Task/Time-a-function/Perl/time-a-function-1.pl
Normal file
18
Task/Time-a-function/Perl/time-a-function-1.pl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
use Benchmark;
|
||||
use Memoize;
|
||||
|
||||
sub fac1 {
|
||||
my $n = shift;
|
||||
return $n == 0 ? 1 : $n * fac1($n - 1);
|
||||
}
|
||||
sub fac2 {
|
||||
my $n = shift;
|
||||
return $n == 0 ? 1 : $n * fac2($n - 1);
|
||||
}
|
||||
memoize('fac2');
|
||||
|
||||
my $result = timethese(100000, {
|
||||
'fac1' => sub { fac1(50) },
|
||||
'fac2' => sub { fac2(50) },
|
||||
});
|
||||
Benchmark::cmpthese($result);
|
||||
26
Task/Time-a-function/Perl/time-a-function-2.pl
Normal file
26
Task/Time-a-function/Perl/time-a-function-2.pl
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
sub cpu_time {
|
||||
my ($user,$system,$cuser,$csystem) = times;
|
||||
$user + $system
|
||||
}
|
||||
|
||||
sub time_it {
|
||||
my $action = shift;
|
||||
my $startTime = cpu_time();
|
||||
$action->(@_);
|
||||
my $finishTime = cpu_time();
|
||||
$finishTime - $startTime
|
||||
}
|
||||
|
||||
printf "Identity(4) takes %f seconds.\n", time_it(sub {@_}, 4);
|
||||
# outputs "Identity(4) takes 0.000000 seconds."
|
||||
|
||||
sub sum {
|
||||
my $x = shift;
|
||||
foreach (0 .. 999999) {
|
||||
$x += $_;
|
||||
}
|
||||
$x
|
||||
}
|
||||
|
||||
printf "Sum(4) takes %f seconds.\n", time_it(\&sum, 4);
|
||||
# outputs "Sum(4) takes 0.280000 seconds."
|
||||
23
Task/Time-a-function/Phix/time-a-function.phix
Normal file
23
Task/Time-a-function/Phix/time-a-function.phix
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
(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>
|
||||
<!--
|
||||
9
Task/Time-a-function/Phixmonti/time-a-function.phixmonti
Normal file
9
Task/Time-a-function/Phixmonti/time-a-function.phixmonti
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def count
|
||||
for drop endfor
|
||||
enddef
|
||||
|
||||
1000000 count
|
||||
msec dup var t0 print " seconds" print nl
|
||||
|
||||
10000000 count
|
||||
msec t0 - print " seconds" print
|
||||
29
Task/Time-a-function/Picat/time-a-function.picat
Normal file
29
Task/Time-a-function/Picat/time-a-function.picat
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
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]).
|
||||
3
Task/Time-a-function/PicoLisp/time-a-function.l
Normal file
3
Task/Time-a-function/PicoLisp/time-a-function.l
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
: (bench (do 1000000 (* 3 4)))
|
||||
0.080 sec
|
||||
-> 12
|
||||
12
Task/Time-a-function/Pike/time-a-function.pike
Normal file
12
Task/Time-a-function/Pike/time-a-function.pike
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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);
|
||||
}
|
||||
12
Task/Time-a-function/PowerShell/time-a-function.psh
Normal file
12
Task/Time-a-function/PowerShell/time-a-function.psh
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
function fun($n){
|
||||
$res = 0
|
||||
if($n -gt 0) {
|
||||
1..$n | foreach{
|
||||
$a, $b = $_, ($n+$_)
|
||||
$res += $a + $b
|
||||
}
|
||||
|
||||
}
|
||||
$res
|
||||
}
|
||||
"$((Measure-Command {fun 10000}).TotalSeconds) Seconds"
|
||||
22
Task/Time-a-function/PureBasic/time-a-function-1.basic
Normal file
22
Task/Time-a-function/PureBasic/time-a-function-1.basic
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
Procedure Foo(Limit)
|
||||
Protected i, palindromic, String$
|
||||
For i=0 To Limit
|
||||
String$=Str(i)
|
||||
If String$=ReverseString(String$)
|
||||
palindromic+1
|
||||
EndIf
|
||||
Next
|
||||
ProcedureReturn palindromic
|
||||
EndProcedure
|
||||
|
||||
If OpenConsole()
|
||||
Define Start, Stop, cnt
|
||||
PrintN("Starting timing of a calculation,")
|
||||
PrintN("for this we test how many of 0-1000000 are palindromic.")
|
||||
Start=ElapsedMilliseconds()
|
||||
cnt=Foo(1000000)
|
||||
Stop=ElapsedMilliseconds()
|
||||
PrintN("The function need "+Str(stop-Start)+" msec,")
|
||||
PrintN("and "+Str(cnt)+" are palindromic.")
|
||||
Print("Press ENTER to exit."): Input()
|
||||
EndIf
|
||||
14
Task/Time-a-function/PureBasic/time-a-function-2.basic
Normal file
14
Task/Time-a-function/PureBasic/time-a-function-2.basic
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
If OpenConsole()
|
||||
Define Timed.f, cnt
|
||||
PrintN("Starting timing of a calculation,")
|
||||
PrintN("for this we test how many of 0-1000000 are palindromic.")
|
||||
; Dependent on Droopy-library
|
||||
If MeasureHiResIntervalStart()
|
||||
; Same Foo() as above...
|
||||
cnt=Foo(1000000)
|
||||
Timed=MeasureHiResIntervalStop()
|
||||
EndIf
|
||||
PrintN("The function need "+StrF(Timed*1000,3)+" msec,")
|
||||
PrintN("and "+Str(cnt)+" are palindromic.")
|
||||
Print("Press ENTER to exit."): Input()
|
||||
EndIf
|
||||
30
Task/Time-a-function/PureBasic/time-a-function-3.basic
Normal file
30
Task/Time-a-function/PureBasic/time-a-function-3.basic
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
Procedure.f ticksHQ(reportIfPresent = #False)
|
||||
Static maxfreq.q
|
||||
Protected T.q
|
||||
If reportIfPresent Or maxfreq = 0
|
||||
QueryPerformanceFrequency_(@maxfreq)
|
||||
If maxfreq
|
||||
ProcedureReturn 1.0
|
||||
Else
|
||||
ProcedureReturn 0
|
||||
EndIf
|
||||
EndIf
|
||||
QueryPerformanceCounter_(@T)
|
||||
ProcedureReturn T / maxfreq ;Result is in milliseconds
|
||||
EndProcedure
|
||||
|
||||
If OpenConsole()
|
||||
Define timed.f, cnt
|
||||
PrintN("Starting timing of a calculation,")
|
||||
PrintN("for this we test how many of 0-1000000 are palindromic.")
|
||||
; Dependent on Windows API
|
||||
If ticksHQ(#True)
|
||||
timed = ticksHQ() ;start time
|
||||
; Same Foo() as above...
|
||||
cnt = Foo(1000000)
|
||||
timed = ticksHQ() - timed ;difference
|
||||
EndIf
|
||||
PrintN("The function need " + StrF(timed * 1000, 3) + " msec,")
|
||||
PrintN("and " + Str(cnt) + " are palindromic.")
|
||||
Print("Press ENTER to exit."): Input()
|
||||
EndIf
|
||||
19
Task/Time-a-function/Python/time-a-function.py
Normal file
19
Task/Time-a-function/Python/time-a-function.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import sys, timeit
|
||||
def usec(function, arguments):
|
||||
modname, funcname = __name__, function.__name__
|
||||
timer = timeit.Timer(stmt='%(funcname)s(*args)' % vars(),
|
||||
setup='from %(modname)s import %(funcname)s; args=%(arguments)r' % vars())
|
||||
try:
|
||||
t, N = 0, 1
|
||||
while t < 0.2:
|
||||
t = min(timer.repeat(repeat=3, number=N))
|
||||
N *= 10
|
||||
microseconds = round(10000000 * t / N, 1) # per loop
|
||||
return microseconds
|
||||
except:
|
||||
timer.print_exc(file=sys.stderr)
|
||||
raise
|
||||
|
||||
from math import pow
|
||||
def nothing(): pass
|
||||
def identity(x): return x
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue