blob: f2ecd1d443f6989bf758b944bcb4b83b44c3e583 [file] [log] [blame]
Elliott Hughes97aba272013-02-13 16:21:51 -08001/*
2 * Copyright (C) 2010 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package benchmarks;
18
Elliott Hughes97aba272013-02-13 16:21:51 -080019import java.util.HashMap;
20import java.util.Hashtable;
21import java.util.LinkedHashMap;
Paul Duffinea13f822015-11-30 11:09:55 +000022import java.util.concurrent.ConcurrentHashMap;
Elliott Hughes97aba272013-02-13 16:21:51 -080023
24/**
25 * How do the various hash maps compare?
26 */
Paul Duffinea13f822015-11-30 11:09:55 +000027public class HashedCollectionsBenchmark {
Elliott Hughes97aba272013-02-13 16:21:51 -080028 public void timeHashMapGet(int reps) {
29 HashMap<String, String> map = new HashMap<String, String>();
30 map.put("hello", "world");
31 for (int i = 0; i < reps; ++i) {
32 map.get("hello");
33 }
34 }
35 public void timeHashMapGet_Synchronized(int reps) {
36 HashMap<String, String> map = new HashMap<String, String>();
37 synchronized (map) {
38 map.put("hello", "world");
39 }
40 for (int i = 0; i < reps; ++i) {
41 synchronized (map) {
42 map.get("hello");
43 }
44 }
45 }
46 public void timeHashtableGet(int reps) {
47 Hashtable<String, String> map = new Hashtable<String, String>();
48 map.put("hello", "world");
49 for (int i = 0; i < reps; ++i) {
50 map.get("hello");
51 }
52 }
53 public void timeLinkedHashMapGet(int reps) {
54 LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
55 map.put("hello", "world");
56 for (int i = 0; i < reps; ++i) {
57 map.get("hello");
58 }
59 }
60 public void timeConcurrentHashMapGet(int reps) {
61 ConcurrentHashMap<String, String> map = new ConcurrentHashMap<String, String>();
62 map.put("hello", "world");
63 for (int i = 0; i < reps; ++i) {
64 map.get("hello");
65 }
66 }
67}