summaryrefslogtreecommitdiff
path: root/test/files/run/collections.scala
blob: 5e97b2df3857520b9612247ce535a632e0c6e2ab (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import collection._

object Test extends Application {

  val printTime = false

  def sum[A](xs: Iterable[int]) = (0 /: xs)((x, y) => x + y)

  def time(op: => unit): unit = {
    val start = System.currentTimeMillis;
    op
    if (printTime) Console.println("  time = "+(System.currentTimeMillis - start)+"ms")
  }

  def test(msg: String, s0: collection.immutable.Set[int]) = {
    Console.println("***** "+msg+":")
    var s = s0
    s = s + 2
    s = s + (3, 4000, 10000)
    Console.println("test1: "+sum(s))
    time {
      s = s ++ (List.range(0, 5000) map (2*))
      Console.println("test2: "+sum(s))
    }
    time {
      var x = 0
      for (val i <- (0 to 10000))
        if (s contains i) x = x + i
      Console.println("test3: "+x)
    }
  }

  def test(msg: String, s0: collection.mutable.Set[int]) = {
    Console.println("***** "+msg+":")
    var s = s0
    s = s + 2
    s = s + (3, 4000, 10000)
    Console.println("test1: "+sum(s))
    time {
      s = s ++ (List.range(0, 5000) map (2*))
      Console.println("test2: "+sum(s))
    }
    time {
      var x = 0
      for (val i <- (0 to 10000))
        if (s contains i) x = x + i
      Console.println("test3: "+x)
    }
  }

  def test(msg: String, s0: collection.immutable.Map[int, int]) = {
    Console.println("***** "+msg+":")
    var s = s0
    s = s + (2 -> 2)
    s = s + (3 -> 3, 4000 -> 4000, 10000 -> 10000)
    Console.println("test1: "+sum(s map (._2)))
    time {
      s = s ++ (List.range(0, 1000) map (x => x * 2 -> x * 2))
      Console.println("test2: "+sum(s map (._2)))
    }
    time {
      var x = 0
      for (val i <- (0 to 10000))
        s get i match {
          case Some(i) => x = x + i
          case None =>
        }
      Console.println("test3: "+x)
    }
  }

  def test(msg: String, s0: collection.mutable.Map[int, int]) = {
    Console.println("***** "+msg+":")
    var s = s0
    s = s + (2 -> 2)
    s = s + (3 -> 3, 4000 -> 4000, 10000 -> 10000)
    Console.println("test1: "+sum(s map (._2)))
    time {
      s = s ++ (List.range(0, 5000) map (x => x * 2 -> x * 2))
      Console.println("test2: "+sum(s map (._2)))
    }
    time {
      var x = 0
      for (val i <- (0 to 10000))
        s get i match {
          case Some(i) => x = x + i
          case None =>
        }
      Console.println("test3: "+x)
    }
  }

  test("immutable.ListSet", new immutable.ListSet[int])
  test("immutable.TreeSet", new immutable.TreeSet[int])
  test("mutable.HashSet", new mutable.HashSet[int])
  test("immutable.ListMap", new immutable.ListMap[int, int])
  test("immutable.TreeMap", new immutable.TreeMap[int, int])
  test("immutable.UnBalancedTreeMap", new immutable.UnbalancedTreeMap[int, int])
  test("mutable.HashMap", new mutable.HashMap[int, int])
}