All Downloads are FREE. Search and download functionalities are using the official Maven repository.

test.edu.umd.cloud9.util.benchmark.BenchmarkRandomWalk1HashMapIntInt Maven / Gradle / Ivy

There is a newer version: 2.0.1
Show newest version
package edu.umd.cloud9.util.benchmark;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class BenchmarkRandomWalk1HashMapIntInt {

	private static int removals = 0;

	public static void main(String[] args) {
		int size = 10000000;
		long startTime;
		long duration;
		Random r = new Random();

		System.out.println("Benchmarking HashMap...");
		Map map = new HashMap();
		startTime = System.currentTimeMillis();
		for (int i = 0; i < size; i++) {
			int k = r.nextInt(1000);
			boolean increment = r.nextBoolean();
			if (increment) {
				increment(map, k);
			} else {
				decrement(map, k);

			}
		}
		duration = System.currentTimeMillis() - startTime;

		System.out.println("removals: " + removals);
		System.out.println("Time taken: " + duration + " ms");
	}

	private static void increment(Map map, int key) {
		if (map.containsKey(key)) {
			map.put(key, map.get(key) + 1);
		} else {
			map.put(key, 1);
		}
	}

	private static void decrement(Map map, int key) {
		if (map.containsKey(key)) {
			int val = map.get(key);
			if (val == 1) {
				removals++;
				map.remove(key);
			} else {
				map.put(key, val - 1);
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy