|
Java example source code file (HashSetvBitSetTest.java)
The HashSetvBitSetTest.java Java example source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work 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. */ package org.apache.commons.lang3; import java.util.BitSet; import java.util.HashSet; import org.junit.Assert; import org.junit.Test; /** * Test to show whether using BitSet for removeAll() methods is faster than using HashSet. */ public class HashSetvBitSetTest { private static final int LOOPS = 2000; // number of times to invoke methods private static final int LOOPS2 = 10000; @Test public void testTimes() { timeHashSet(10); // warmup timeBitSet(10); // warmup long timeDiff = printTimes(0); timeDiff += printTimes(5); timeDiff += printTimes(10); timeDiff += printTimes(200); timeDiff += printTimes(50); timeDiff += printTimes(100); timeDiff += printTimes(1000); timeDiff += printTimes(2000); Assert.assertTrue(timeDiff <= 0); } /** * @return bitSet - HashSet */ private long printTimes(final int count) { final long hashSet = timeHashSet(count); final long bitSet = timeBitSet(count); // If percent is less than 100, then bitset is faster System.out.println("Ratio="+(bitSet*100/hashSet)+"% count="+count+" hash="+hashSet+" bits="+bitSet); return bitSet - hashSet; } private static long timeHashSet(final int count) { int [] result = new int[0]; final long start = System.nanoTime(); for (int i = 0; i < LOOPS; i++) { result = testHashSet(count); } final long elapsed = System.nanoTime() - start; Assert.assertEquals(count, result.length); return elapsed; } private static long timeBitSet(final int count) { int [] result = new int[0]; final long start = System.nanoTime(); for (int i = 0; i < LOOPS; i++) { result = testBitSet(count); } final long elapsed = System.nanoTime() - start; Assert.assertEquals(count, result.length); return elapsed; } @SuppressWarnings("boxing") private static int[] testHashSet(final int count) { final HashSet<Integer> toRemove = new HashSet Other Java examples (source code examples)Here is a short list of links related to this Java HashSetvBitSetTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.