summaryrefslogtreecommitdiff
path: root/src/main/java/org/linaro/benchmarks/micro/HashMapBench.java
blob: 02ffe14d592c91044970b39eec828e5129cea5ff (plain)
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
101
/*
 * Copyright (c) 2015, Linaro Limited. Ported to Java from:
 *  http://browserbench.org/JetStream/sources/hash-map.js
 *
 * Description:   A benchmark case for hash map
 */

/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE below for additional
 *  information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/******* NOTICE *********

 Apache Harmony
 Copyright 2006, 2010 The Apache Software Foundation.

 This product includes software developed at
 The Apache Software Foundation (http://www.apache.org/).

 Portions of Apache Harmony were originally developed by
 Intel Corporation and are licensed to the Apache Software
 Foundation under the "Software Grant and Corporate Contribution
 License Agreement" and for which the following copyright notices
 apply
 (C) Copyright 2005 Intel Corporation
 (C) Copyright 2005-2006 Intel Corporation
 (C) Copyright 2006 Intel Corporation


 The following copyright notice(s) were affixed to portions of the code
 with which this file is now or was at one time distributed
 and are placed here unaltered.

 (C) Copyright 1997,2004 International Business Machines Corporation.
 All rights reserved.

 (C) Copyright IBM Corp. 2003.


 This software contains code derived from UNIX V7, Copyright(C)
 Caldera International Inc.

 ************************/

package org.linaro.benchmarks.micro;

import java.util.HashMap;
import java.util.Map;
import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)

public class HashMapBench {

  private static final int COUNT = 5000;
  private static final int resultExpect = 1050000;
  private static final long keySumExpect = 12497500;
  private static final int valueSumExpect = 210000;
  private int result = 0;
  private long keySum = 0;
  private int valueSum = 0;

  private Map<Integer, Integer> map = new HashMap<Integer, Integer>();

  @Benchmark
  public void jmhTimeTestHashMap() {
    for (int j = 0; j < COUNT; j++) {
      map.put(j, 42);
    }

    result = 0;
    for (int k = 0; k < 5; k++) {
      for (int j = 0; j < COUNT; j++) {
        result += map.get(j);
      }
    }

    keySum = 0;
    valueSum = 0;
    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
      keySum += entry.getKey();
      valueSum += entry.getValue();
    }
  }
}