Hey, everybody!
I discovered Kappa’s Bag code and decided to put it some use against an ArrayList. So far, I’m very happy with the results! The code for his data structure, if you haven’t seen it yet, is here:
I wrote a little test program to test just simple functionality out of it here (not a realistic example, by any means, but just something for some quick benchmarks, I suppose):
package test;
import java.util.ArrayList;
import java.util.Random;
import util.Bag;
public class BagTest
{
public static void main(String args[])
{
Bag stringBag = new Bag();
ArrayList stringList = new ArrayList();
Random r = new Random();
long time = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++)
{
stringBag.add(r.nextInt(2) == 1 ? "hello" : "goodbye");
}
System.out.println("Adding to the Bag: " + ((System.currentTimeMillis() - time) / 1000.0) + "s");
time = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++)
{
stringList.add(r.nextInt(2) == 1 ? "hello" : "goodbye");
}
System.out.println("Adding to the ArrayList: " + ((System.currentTimeMillis() - time) / 1000.0) + "s");
time = System.currentTimeMillis();
while (!stringBag.isEmpty())
{
int remover = r.nextInt(stringBag.size());
String s = (String)stringBag.get(remover);
if (s.equals("goodbye"))
{
stringBag.remove(remover);
}
else
{
stringBag.remove(remover);
stringBag.add("goodbye");
}
}
System.out.println("Removing from the Bag: " + ((System.currentTimeMillis() - time) / 1000.0) + "s");
time = System.currentTimeMillis();
while (!stringList.isEmpty())
{
int remover = r.nextInt(stringList.size());
String s = (String)stringList.get(remover);
if (s.equals("goodbye"))
{
stringList.remove(remover);
}
else
{
stringList.remove(remover);
stringList.add("goodbye");
}
}
System.out.println("Removing from the ArrayList: " + ((System.currentTimeMillis() - time) / 1000.0) + "s");
}
}
It runs fine with test cases up to 100000 entries (during which, by the way, Bag seems to always take the cake with this test), but when I try to add on another factor of 10 by raising the count to 1000000 (I haven’t tested it in between yet), the last bit where it counts ArrayList removing entries never appears, as if the app has gone into some sort of infinite loop or something without any error messages. I’m not entirely sure why this is, so I thought I’d ask to see if you guys did!
Btw, I did make sure to set -Xss256m and -Xms256m in case this was the issue, but this didn’t help either.
Thanks a bunch!
Best regards,
Colton