Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
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
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
atom t0 = time()
|
||||
some_procedure()
|
||||
printf(1,"%3.2fs.\n",time()-t0)
|
||||
printf(1,"%3.2fs\n",time()-t0)
|
||||
printf(1,"%s\n",{elapsed(time()-t0)})
|
||||
|
|
|
|||
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);
|
||||
}
|
||||
|
|
@ -14,5 +14,5 @@ def usec(function, arguments):
|
|||
timer.print_exc(file=sys.stderr)
|
||||
raise
|
||||
|
||||
def nothing(): pass
|
||||
def identity(x): return x
|
||||
def nothing(): pass
|
||||
def identity(x): return x
|
||||
|
|
|
|||
56
Task/Time-a-function/Swift/time-a-function.swift
Normal file
56
Task/Time-a-function/Swift/time-a-function.swift
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
import Foundation
|
||||
|
||||
public struct TimeResult {
|
||||
public var seconds: Double
|
||||
public var nanoSeconds: Double
|
||||
|
||||
public var duration: Double { seconds + (nanoSeconds / 1e9) }
|
||||
|
||||
@usableFromInline
|
||||
init(seconds: Double, nanoSeconds: Double) {
|
||||
self.seconds = seconds
|
||||
self.nanoSeconds = nanoSeconds
|
||||
}
|
||||
}
|
||||
|
||||
extension TimeResult: CustomStringConvertible {
|
||||
public var description: String {
|
||||
return "TimeResult(seconds: \(seconds); nanoSeconds: \(nanoSeconds); duration: \(duration)s)"
|
||||
}
|
||||
}
|
||||
|
||||
public struct ClockTimer {
|
||||
@inlinable @inline(__always)
|
||||
public static func time<T>(_ f: () throws -> T) rethrows -> (T, TimeResult) {
|
||||
var tsi = timespec()
|
||||
var tsf = timespec()
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC_RAW, &tsi)
|
||||
let res = try f()
|
||||
clock_gettime(CLOCK_MONOTONIC_RAW, &tsf)
|
||||
|
||||
let secondsElapsed = difftime(tsf.tv_sec, tsi.tv_sec)
|
||||
let nanoSecondsElapsed = Double(tsf.tv_nsec - tsi.tv_nsec)
|
||||
|
||||
return (res, TimeResult(seconds: secondsElapsed, nanoSeconds: nanoSecondsElapsed))
|
||||
}
|
||||
}
|
||||
|
||||
func ackermann(m: Int, n: Int) -> Int {
|
||||
switch (m, n) {
|
||||
case (0, _):
|
||||
return n + 1
|
||||
case (_, 0):
|
||||
return ackermann(m: m - 1, n: 1)
|
||||
case (_, _):
|
||||
return ackermann(m: m - 1, n: ackermann(m: m, n: n - 1))
|
||||
}
|
||||
}
|
||||
|
||||
let (n, t) = ClockTimer.time { ackermann(m: 3, n: 11) }
|
||||
|
||||
print("Took \(t.duration)s to calculate ackermann(m: 3, n: 11) = \(n)")
|
||||
|
||||
let (n2, t2) = ClockTimer.time { ackermann(m: 4, n: 1) }
|
||||
|
||||
print("Took \(t2.duration)s to calculate ackermann(m: 4, n: 1) = \(n2)")
|
||||
Loading…
Add table
Add a link
Reference in a new issue