Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
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
|
||||
19
Task/Time-a-function/FreeBASIC/time-a-function.freebasic
Normal file
19
Task/Time-a-function/FreeBASIC/time-a-function.freebasic
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
|
||||
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"
|
||||
14
Task/Time-a-function/Nim/time-a-function.nim
Normal file
14
Task/Time-a-function/Nim/time-a-function.nim
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import times, os
|
||||
|
||||
proc doWork(x) =
|
||||
var n = x
|
||||
for i in 0..10000000:
|
||||
n += i
|
||||
echo n
|
||||
|
||||
template time(s: stmt): expr =
|
||||
let t0 = cpuTime()
|
||||
s
|
||||
cpuTime() - t0
|
||||
|
||||
echo time(doWork(100))
|
||||
3
Task/Time-a-function/Phix/time-a-function.phix
Normal file
3
Task/Time-a-function/Phix/time-a-function.phix
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
atom t0 = time()
|
||||
some_procedure()
|
||||
printf(1,"%3.2fs.\n",time()-t0)
|
||||
7
Task/Time-a-function/Ring/time-a-function.ring
Normal file
7
Task/Time-a-function/Ring/time-a-function.ring
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
beginTime = TimeList()[13]
|
||||
for n = 1 to 10000000
|
||||
n = n + 1
|
||||
next
|
||||
endTime = TimeList()[13]
|
||||
elapsedTime = endTime - beginTime
|
||||
see "Elapsed time = " + elapsedTime + nl
|
||||
20
Task/Time-a-function/Sidef/time-a-function.sidef
Normal file
20
Task/Time-a-function/Sidef/time-a-function.sidef
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
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)
|
||||
3
Task/Time-a-function/Wart/time-a-function.wart
Normal file
3
Task/Time-a-function/Wart/time-a-function.wart
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
time 1+1
|
||||
30000/1000000 # in microseconds
|
||||
=> 2
|
||||
Loading…
Add table
Add a link
Reference in a new issue