-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
687090f
commit 4239238
Showing
6 changed files
with
193 additions
and
8 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# This code is derived from the SOM benchmarks | ||
# | ||
# Based on the JavaScript implementation at | ||
# https://github.com/smarr/are-we-fast-yet/blob/master/benchmarks/JavaScript/list.js | ||
|
||
class Element { | ||
Element(v){ | ||
self.val = v | ||
self.next = nil | ||
} | ||
|
||
length() { | ||
if self.next == nil { | ||
return 1 | ||
} | ||
|
||
return 1 + self.next.length() | ||
} | ||
} | ||
|
||
class List { | ||
benchmark() { | ||
var result = self.tail( | ||
self.make_list(15), | ||
self.make_list(10), | ||
self.make_list(6) | ||
) | ||
|
||
return result.length() | ||
} | ||
|
||
make_list(length) { | ||
if length == 0 { | ||
return nil | ||
} | ||
|
||
var e = Element(length) | ||
e.next = self.make_list(length - 1) | ||
return e | ||
} | ||
|
||
is_shorter_than(x, y) { | ||
var x_tail = x | ||
var y_tail = y | ||
|
||
while y_tail != nil { | ||
if x_tail == nil { | ||
return true | ||
} | ||
|
||
x_tail = x_tail.next | ||
y_tail = y_tail.next | ||
} | ||
|
||
return false | ||
} | ||
|
||
tail(x, y, z) { | ||
if self.is_shorter_than(y, x) { | ||
return self.tail( | ||
self.tail(x.next, y, z), | ||
self.tail(y.next, z, x), | ||
self.tail(z.next, x, y) | ||
) | ||
} | ||
|
||
return z | ||
} | ||
} | ||
|
||
var start = microtime() | ||
echo List().benchmark() == 10 | ||
var end = microtime() | ||
|
||
echo 'Time taken = ${(end - start) / 1000000} seconds' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* The Computer Language Benchmarks Game | ||
* https://salsa.debian.org/benchmarksgame-team/benchmarksgame/ | ||
* | ||
* contributed by Richard Ore | ||
* based on contributed work by Ziad Hatahet | ||
* based on the Go entry by K P anonymous | ||
* | ||
* This implementation of the spectral norm employs multithreading | ||
* to achieve more than 10x the speed over the ordinary version | ||
* (depending on how many cores your cpu has). | ||
*/ | ||
|
||
import thread | ||
|
||
def spectralnorm(n) { | ||
if !n n = 100 | ||
|
||
var u = [1] * n | ||
var v = [0] * n | ||
|
||
for i in 0..10 { | ||
a_times_transp(v, u) | ||
a_times_transp(u, v) | ||
} | ||
|
||
var vbv = 0, vv = 0 | ||
for i in 0..n { | ||
var vi = v[i] | ||
vbv += u[i] * vi | ||
vv += vi ** 2 | ||
} | ||
|
||
return ((vbv / vv) ** 0.5) | ||
} | ||
|
||
def a_times_transp(v, u) { | ||
var x = [0] * u.length() | ||
var t = [] | ||
var ncpu = thread.cpu_count | ||
|
||
for i in 0..ncpu { | ||
t.append(thread.start( | ||
times, | ||
[ | ||
x, | ||
i * v.length() / ncpu, | ||
(i + 1) * v.length() / ncpu, | ||
u, | ||
false, | ||
] | ||
)) | ||
} | ||
|
||
for i in 0..ncpu { | ||
t[i].await() | ||
} | ||
|
||
for i in 0..ncpu { | ||
t[i] = thread.start( | ||
times, | ||
[ | ||
v, | ||
i * v.length() / ncpu, | ||
(i + 1) * v.length() / ncpu, | ||
x, | ||
true, | ||
] | ||
) | ||
} | ||
|
||
for i in 0..ncpu { | ||
t[i].await() | ||
} | ||
} | ||
|
||
def times(_, v, ii, n, u, transpose) { | ||
var ul = u.length() | ||
for i in ii..n { | ||
var vi = 0 | ||
for j in 0..ul { | ||
if transpose { | ||
vi += u[j] / a(j, i) | ||
} else { | ||
vi += u[j] / a (i, j) | ||
} | ||
} | ||
|
||
v[i] = vi | ||
} | ||
} | ||
|
||
def a(i, j) { | ||
return (i + j) * (i + j + 1) / 2 + i + 1 | ||
} | ||
|
||
var start = microtime() | ||
echo spectralnorm(5500) | ||
var end = microtime() | ||
|
||
echo '\nTime taken = ${(end - start) / 1000000} seconds' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters