all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
5
Task/Time-a-function/0DESCRIPTION
Normal file
5
Task/Time-a-function/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
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]].
|
||||
2
Task/Time-a-function/1META.yaml
Normal file
2
Task/Time-a-function/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Programming environment operations
|
||||
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)))
|
||||
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)
|
||||
{
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
integer
|
||||
sum(integer c)
|
||||
{
|
||||
integer s;
|
||||
|
||||
s = 0;
|
||||
while (c) {
|
||||
s += c;
|
||||
c -= 1;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
real
|
||||
time_f(integer (*fp) (integer), integer fa)
|
||||
{
|
||||
date f, s;
|
||||
time t;
|
||||
|
||||
d_now(s);
|
||||
|
||||
fp(fa);
|
||||
|
||||
d_now(f);
|
||||
|
||||
t_ddiff(t, f, s);
|
||||
|
||||
return t_microsecond(t) / 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");
|
||||
|
||||
return 0;
|
||||
}
|
||||
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"
|
||||
}
|
||||
14
Task/Time-a-function/AutoHotkey/time-a-function-2.ahk
Normal file
14
Task/Time-a-function/AutoHotkey/time-a-function-2.ahk
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
MsgBox % time("fx")
|
||||
|
||||
time(function, parameter=0){
|
||||
SetBatchLines -1
|
||||
DllCall("QueryPerformanceCounter", "Int64*", CounterBefore)
|
||||
DllCall("QueryPerformanceFrequency", "Int64*", Freq)
|
||||
%function%(parameter)
|
||||
DllCall("QueryPerformanceCounter", "Int64*", CounterAfter)
|
||||
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
|
||||
3
Task/Time-a-function/BBC-BASIC/time-a-function.bbc
Normal file
3
Task/Time-a-function/BBC-BASIC/time-a-function.bbc
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
start%=TIME:REM centi-second timer
|
||||
REM perform processing
|
||||
lapsed%=TIME-start%
|
||||
23
Task/Time-a-function/C++/time-a-function.cpp
Normal file
23
Task/Time-a-function/C++/time-a-function.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;
|
||||
}
|
||||
34
Task/Time-a-function/C/time-a-function.c
Normal file
34
Task/Time-a-function/C/time-a-function.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#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;
|
||||
}
|
||||
|
||||
#define CLOCKTYPE CLOCK_MONOTONIC
|
||||
/* this one should be appropriate to avoid errors on multiprocessors systems */
|
||||
|
||||
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;
|
||||
}
|
||||
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`)
|
||||
}
|
||||
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)
|
||||
1
Task/Time-a-function/Factor/time-a-function.factor
Normal file
1
Task/Time-a-function/Factor/time-a-function.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
[ 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
|
||||
17
Task/Time-a-function/Fortran/time-a-function.f
Normal file
17
Task/Time-a-function/Fortran/time-a-function.f
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
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,max
|
||||
call system_clock(start,rate,max)
|
||||
c Here comes the function we want to time
|
||||
call do_something()
|
||||
call system_clock(finish,rate,max)
|
||||
write(6,*) 'Elapsed Time in seconds:',(finish-start)/rate
|
||||
return
|
||||
end
|
||||
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()
|
||||
}
|
||||
}
|
||||
25
Task/Time-a-function/Go/time-a-function-3.go
Normal file
25
Task/Time-a-function/Go/time-a-function-3.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}"
|
||||
}
|
||||
}
|
||||
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
|
||||
|
||||
-- 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 (\x -> f x `seq` return ())
|
||||
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)
|
||||
2
Task/Time-a-function/J/time-a-function.j
Normal file
2
Task/Time-a-function/J/time-a-function.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(6!:2,7!:2) '|: 50 50 50 $ i. 50^3'
|
||||
0.00387912 1.57414e6
|
||||
27
Task/Time-a-function/Java/time-a-function-1.java
Normal file
27
Task/Time-a-function/Java/time-a-function-1.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-2.java
Normal file
12
Task/Time-a-function/Java/time-a-function-2.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");
|
||||
|
||||
}
|
||||
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 ) )
|
||||
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/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.pari
Normal file
4
Task/Time-a-function/PARI-GP/time-a-function.pari
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
time(foo)={
|
||||
foo();
|
||||
gettime()
|
||||
};
|
||||
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. */
|
||||
3
Task/Time-a-function/Perl-6/time-a-function.pl6
Normal file
3
Task/Time-a-function/Perl-6/time-a-function.pl6
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
my $start = now;
|
||||
(^100000).pick(1000);
|
||||
say now - $start;
|
||||
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."
|
||||
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
|
||||
22
Task/Time-a-function/PureBasic/time-a-function-1.purebasic
Normal file
22
Task/Time-a-function/PureBasic/time-a-function-1.purebasic
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.purebasic
Normal file
14
Task/Time-a-function/PureBasic/time-a-function-2.purebasic
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.purebasic
Normal file
30
Task/Time-a-function/PureBasic/time-a-function-3.purebasic
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
|
||||
18
Task/Time-a-function/Python/time-a-function.py
Normal file
18
Task/Time-a-function/Python/time-a-function.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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
|
||||
|
||||
def nothing(): pass
|
||||
def identity(x): return x
|
||||
13
Task/Time-a-function/R/time-a-function-1.r
Normal file
13
Task/Time-a-function/R/time-a-function-1.r
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# A task
|
||||
foo <- function()
|
||||
{
|
||||
for(i in 1:10)
|
||||
{
|
||||
mat <- matrix(rnorm(1e6), nrow=1e3)
|
||||
mat^-0.5
|
||||
}
|
||||
}
|
||||
# Time the task
|
||||
timer <- system.time(foo())
|
||||
# Extract the processing time
|
||||
timer["user.self"]
|
||||
4
Task/Time-a-function/R/time-a-function-2.r
Normal file
4
Task/Time-a-function/R/time-a-function-2.r
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
Rprof()
|
||||
foo()
|
||||
Rprof(NULL)
|
||||
summaryRprof()
|
||||
31
Task/Time-a-function/REXX/time-a-function-1.rexx
Normal file
31
Task/Time-a-function/REXX/time-a-function-1.rexx
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/*REXX program to show the elapsed time for a function (or subroutine). */
|
||||
arg times . /*get the arg from command line. */
|
||||
if times=='' then times=100000 /*any specified? No, use default*/
|
||||
|
||||
call time 'R' /*only 1st character is examined.*/
|
||||
call time 'reset' /*this verbose version also works*/
|
||||
call time 'rompishness' /*and yet another example. */
|
||||
|
||||
junk = silly(times) /*invoke the SILLY function. */
|
||||
/* CALL SILLY TIMES also works.*/
|
||||
|
||||
/*The E is for elapsed time.*/
|
||||
/* │ */
|
||||
/* ┌────────┘ */
|
||||
/* │ */
|
||||
/* ↓ */
|
||||
say 'function SILLY took' format(time("E"),,2) 'seconds for' times "iterations."
|
||||
/* ↑ */
|
||||
/* │ */
|
||||
/* ┌────────────────┘ */
|
||||
/* │ */
|
||||
/* The above 2 for the FORMAT function displays the time */
|
||||
/* with 2 decimal digits (past the decimal point). Using */
|
||||
/* a 0 (zero) would round the time to whole seconds. */
|
||||
exit
|
||||
/*──────────────────────────────────SILLY subroutine────────────────────*/
|
||||
silly: procedure /*chew up some CPU time doing some silly stuff.*/
|
||||
do j=1 for arg(1) /*wash, apply, lather, rinse, repeat. ... */
|
||||
a.j=random() date() time() digits() fuzz() form() xrange() queued()
|
||||
end /*j*/
|
||||
return j-1
|
||||
26
Task/Time-a-function/REXX/time-a-function-2.rexx
Normal file
26
Task/Time-a-function/REXX/time-a-function-2.rexx
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/*REXX program shows the CPU time used for a REXX pgm since it started. */
|
||||
arg times . /*get the arg from command line. */
|
||||
if times=='' then times=100000 /*any specified? No, use default*/
|
||||
junk = silly(times) /*invoke the SILLY function. */
|
||||
/* CALL SILLY TIMES also works.*/
|
||||
|
||||
/*The J is for time used by */
|
||||
/* │ the REXX program */
|
||||
/* ┌────────┘ since it started, */
|
||||
/* │ this is a Regina extension. */
|
||||
/* ↓ */
|
||||
say 'function SILLY took' format(time("J"),,2) 'seconds for' times "iterations."
|
||||
/* ↑ */
|
||||
/* │ */
|
||||
/* ┌────────────────┘ */
|
||||
/* │ */
|
||||
/* The above 2 for the FORMAT function displays the time */
|
||||
/* with 2 decimal digits (past the decimal point). Using */
|
||||
/* a 0 (zero) would round the time to whole seconds. */
|
||||
exit
|
||||
/*──────────────────────────────────SILLY subroutine────────────────────*/
|
||||
silly: procedure /*chew up some CPU time doing some silly stuff.*/
|
||||
do j=1 for arg(1) /*wash, apply, lather, rinse, repeat. ... */
|
||||
a.j=random() date() time() digits() fuzz() form() xrange() queued()
|
||||
end /*j*/
|
||||
return j-1
|
||||
7
Task/Time-a-function/Racket/time-a-function.rkt
Normal file
7
Task/Time-a-function/Racket/time-a-function.rkt
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#lang racket
|
||||
(define (fact n)
|
||||
(if (zero? n)
|
||||
1
|
||||
(* n (fact (sub1 n)))))
|
||||
|
||||
(time (fact 5000))
|
||||
4
Task/Time-a-function/Retro/time-a-function.retro
Normal file
4
Task/Time-a-function/Retro/time-a-function.retro
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
: .runtime ( a- ) time [ do time ] dip - "\n%d\n" puts ;
|
||||
|
||||
: test 20000 [ putn space ] iterd ;
|
||||
&test .runtime
|
||||
6
Task/Time-a-function/Ruby/time-a-function-1.rb
Normal file
6
Task/Time-a-function/Ruby/time-a-function-1.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
require 'benchmark'
|
||||
|
||||
Benchmark.bm(8) do |x|
|
||||
x.report("nothing:") { }
|
||||
x.report("sum:") { (1..1_000_000).inject(4) {|sum, x| sum + x} }
|
||||
end
|
||||
1
Task/Time-a-function/Ruby/time-a-function-2.rb
Normal file
1
Task/Time-a-function/Ruby/time-a-function-2.rb
Normal file
|
|
@ -0,0 +1 @@
|
|||
Benchmark.measure { whatever }.total
|
||||
5
Task/Time-a-function/Scala/time-a-function-1.scala
Normal file
5
Task/Time-a-function/Scala/time-a-function-1.scala
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def time(f: => Unit)={
|
||||
val s = System.currentTimeMillis
|
||||
f
|
||||
System.currentTimeMillis - s
|
||||
}
|
||||
3
Task/Time-a-function/Scala/time-a-function-2.scala
Normal file
3
Task/Time-a-function/Scala/time-a-function-2.scala
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
println(time {
|
||||
for(i <- 1 to 10000000) {}
|
||||
})
|
||||
3
Task/Time-a-function/Scala/time-a-function-3.scala
Normal file
3
Task/Time-a-function/Scala/time-a-function-3.scala
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def count(i:Int) = for(j <- 1 to i){}
|
||||
|
||||
println(time (count(10000000)))
|
||||
1
Task/Time-a-function/Scheme/time-a-function.ss
Normal file
1
Task/Time-a-function/Scheme/time-a-function.ss
Normal file
|
|
@ -0,0 +1 @@
|
|||
(time (some-function))
|
||||
35
Task/Time-a-function/Seed7/time-a-function.seed7
Normal file
35
Task/Time-a-function/Seed7/time-a-function.seed7
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
$ 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;
|
||||
1
Task/Time-a-function/Slate/time-a-function.slate
Normal file
1
Task/Time-a-function/Slate/time-a-function.slate
Normal file
|
|
@ -0,0 +1 @@
|
|||
[inform: 2000 factorial] timeToRun.
|
||||
2
Task/Time-a-function/Smalltalk/time-a-function.st
Normal file
2
Task/Time-a-function/Smalltalk/time-a-function.st
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Time millisecondsToRun: [
|
||||
Transcript show: 2000 factorial ].
|
||||
7
Task/Time-a-function/Standard-ML/time-a-function.ml
Normal file
7
Task/Time-a-function/Standard-ML/time-a-function.ml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
fun time_it (action, arg) = let
|
||||
val timer = Timer.startCPUTimer ()
|
||||
val _ = action arg
|
||||
val times = Timer.checkCPUTimer timer
|
||||
in
|
||||
Time.+ (#usr times, #sys times)
|
||||
end
|
||||
14
Task/Time-a-function/TUSCRIPT/time-a-function.tuscript
Normal file
14
Task/Time-a-function/TUSCRIPT/time-a-function.tuscript
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
$$ 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"
|
||||
7
Task/Time-a-function/Tcl/time-a-function.tcl
Normal file
7
Task/Time-a-function/Tcl/time-a-function.tcl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
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)}]
|
||||
}
|
||||
|
||||
puts [time {sum_n 1e6} 100]
|
||||
puts [time {} 100]
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
function exampleFunction(%var1,%var2)
|
||||
{
|
||||
//put stuff here
|
||||
}
|
||||
|
||||
benchmark(500,"exampleFunction","blah","variables");
|
||||
|
||||
==> BENCHMARKING RESULT FOR exampleFunction:
|
||||
==> 13.257
|
||||
1
Task/Time-a-function/UNIX-Shell/time-a-function.sh
Normal file
1
Task/Time-a-function/UNIX-Shell/time-a-function.sh
Normal file
|
|
@ -0,0 +1 @@
|
|||
$ time sleep 1
|
||||
7
Task/Time-a-function/XPL0/time-a-function.xpl0
Normal file
7
Task/Time-a-function/XPL0/time-a-function.xpl0
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
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");
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue