-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathperformance.ss
81 lines (72 loc) · 1.63 KB
/
performance.ss
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// performance.ss
// Performance measurement in SurgeScript
// Copyright 2018 Alexandre Martins <alemartf(at)gmail(dot)com>
//
//
// A functor is an object that behaves like a function.
// SurgeScript has a syntactic sugar that allows an object to be "called" like a function.
// Example: given an object f, writing
// y = f(x);
// is the same as writing
// y = f.call(x);
//
// Main Application
object "Application"
{
benchmark = spawn("Benchmark");
fib = spawn("Fibonacci");
state "main"
{
t = benchmark(fib(1));
t += benchmark(fib(5));
t += benchmark(fib(10));
t += benchmark(fib(25));
t += benchmark(fib(32));
Console.print("Total time: " + t + " seconds.");
Application.exit();
}
}
// This object measures the performance of functor f
object "Benchmark"
{
fun call(f)
{
Console.write("Computing " + f + " = ");
start = Time.now;
result = f();
elapsed = Time.now - start;
Console.print(result + " \t\t done in " + elapsed + " seconds.");
return elapsed;
}
}
// This functor computes the Fibonacci sequence using an exponential algorithm
object "ExponentialFibonacci"
{
public n = 1;
fun call()
{
return fib(n);
}
fun fib(n)
{
if(n > 2)
return fib(n-1) + fib(n-2);
else
return 1;
}
fun toString()
{
return "ExpFib(" + n + ")";
}
}
// This object generates a Fibonacci functor
object "Fibonacci"
{
fun call(n)
{
fib = spawn("ExponentialFibonacci");
fib.n = n;
return fib;
}
}