@@ -12,37 +12,30 @@ object ParallelParenthesesBalancingRunner:
12
12
Key .exec.maxWarmupRuns := 80 ,
13
13
Key .exec.benchRuns := 120 ,
14
14
Key .verbose := false
15
- ) withWarmer (Warmer .Default ())
15
+ ). withWarmer(Warmer .Default ())
16
16
17
- def main ( args : Array [ String ]) : Unit =
17
+ def main : Unit =
18
18
val length = 100000000
19
19
val threshold = 10000
20
-
21
20
val openParens = Array .fill(length / 4 )('(' )
22
21
val closeParens = Array .fill(length / 4 )(')' )
23
-
24
22
val chars = openParens ++ closeParens ++ openParens ++ closeParens
25
23
// val chars = new Array[Char](length)
26
24
27
- val seqtime = standardConfig measure {
25
+ val seqtime = standardConfig. measure:
28
26
seqResult = ParallelParenthesesBalancing .balance(chars)
29
- }
30
-
31
27
println(s " sequential result = $seqResult" )
32
28
println(s " sequential balancing time: $seqtime" )
33
29
34
- val fjtime = standardConfig measure {
30
+ val fjtime = standardConfig. measure:
35
31
parResult = ParallelParenthesesBalancing .parBalance(chars, threshold)
36
- }
37
-
38
32
println(s " parallel result = $parResult" )
39
33
println(s " parallel balancing time: $fjtime" )
34
+
40
35
println(s " speedup: ${seqtime.value / fjtime.value}" )
41
36
42
37
object ParallelParenthesesBalancing extends ParallelParenthesesBalancingInterface :
43
-
44
- /** Returns `true` iff the parentheses in the input `chars` are balanced.
45
- */
38
+ /* Returns `true` iff the parentheses in the input `chars` are balanced. */
46
39
def balance (chars : Array [Char ]): Boolean = // TODO
47
40
@ annotation.tailrec
48
41
def helper (left : Int , right : Int , chars : Array [Char ]): Boolean =
@@ -55,8 +48,7 @@ object ParallelParenthesesBalancing extends ParallelParenthesesBalancingInterfac
55
48
case _ => helper(left, right, chars.tail)
56
49
helper(0 , 0 , chars)
57
50
58
- /** Returns `true` iff the parentheses in the input `chars` are balanced.
59
- */
51
+ /* Returns `true` iff the parentheses in the input `chars` are balanced. */
60
52
def parBalance (chars : Array [Char ], threshold : Int ): Boolean = // TODO
61
53
@ annotation.tailrec
62
54
def traverse (idx : Int , until : Int , arg1 : Int , arg2 : Int ): (Int , Int ) =
@@ -69,8 +61,7 @@ object ParallelParenthesesBalancing extends ParallelParenthesesBalancingInterfac
69
61
case _ => traverse(idx + 1 , until, arg1, arg2)
70
62
71
63
def reduce (from : Int , until : Int ): (Int , Int ) = // TODO
72
- if until - from <= threshold
73
- then traverse(from, until, 0 , 0 )
64
+ if until - from <= threshold then traverse(from, until, 0 , 0 )
74
65
else
75
66
val mid : Int = (until - from) / 2 + from
76
67
val ((l1, r1), (l2, r2)) = parallel(reduce(from, mid), reduce(mid, until))
0 commit comments