FileDocCategorySizeDatePackage
FilterComparison.javaAPI DocExample8063Fri May 10 09:28:54 BST 2002tuning.string

FilterComparison

public class FilterComparison extends Object

Fields Summary
static char[]
NewLine
static int
count
public static char[]
buf
Constructors Summary
Methods Summary
public static voidcfilter(java.lang.String filter, java.lang.String filename, boolean print)

    MatchReader.filter(filter, filename, print);
  
public static voidfilter1(java.lang.String filter, java.lang.String filename, boolean print)

    count = 0;
//    BufferedReader rdr = new BufferedReader(new FileReader(filename));
    BufferedReader rdr = new BufferedReader(new CharArrayReader(buf));
    String line;
    String ufilter = filter.toUpperCase();
    while( (line = rdr.readLine()) != null)
    {
      if (line.toUpperCase().indexOf(ufilter) != -1)
      {
        count++;
        if (print)
          System.out.println(line);
      }
    }
    System.out.println(count + " lines matched.");
    rdr.close();
  
public static voidfilter2(java.lang.String filter, java.lang.String filename, boolean print)

    count = 0;
//    BufferedReader rdr = new BufferedReader(new FileReader(filename));
    BufferedReader rdr = new BufferedReader(new CharArrayReader(buf));
    String line;
    int filterLength = filter.length();
    while( (line = rdr.readLine()) != null)
    {
      for(int i = 0; i < line.length(); i++)
        if (line.regionMatches(true, i, filter, 0, filterLength))
        {
          count++;
          if (print)
            System.out.println(line);
          break;
        }
    }
    System.out.println(count + " lines matched.");
    rdr.close();
  
public static voidmain(java.lang.String[] args)

    String NewLineStr = System.getProperty("line.separator");
    NewLine = new char[NewLineStr.length()];
    NewLineStr.getChars(0, NewLine.length, NewLine, 0);
  
    test(args);
    if (args.length > 2)
      test(args);
  
public static voidtest(java.lang.String[] args)

    try
    {
      if (buf == null)
      {
        File f = new File(args[1]);
        buf = new char[(int) f.length()];
        FileReader r = new FileReader(args[1]);
        int len = 0, len2;
        while( (len2 = r.read(buf,len,buf.length-len)) > 0) {len+=len2;}
        if (len != buf.length) throw new Exception("Didn't read the whole file");
        r.close();
      }

      long time = System.currentTimeMillis();
      filter1(args[0], args[1], false);
      time = System.currentTimeMillis() - time;
      System.out.println("filter time (millis) = " + time);

      System.gc();
      System.gc();
      System.gc();
      System.gc();

      time = System.currentTimeMillis();
      filter2(args[0], args[1], false);
      time = System.currentTimeMillis() - time;
      System.out.println("filter time (millis) = " + time);

      System.gc();
      System.gc();
      System.gc();
      System.gc();

      time = System.currentTimeMillis();
      cfilter(args[0], args[1], false);
      time = System.currentTimeMillis() - time;
      System.out.println("cfilter time (millis) = " + time);

      System.gc();
      System.gc();
      System.gc();
      System.gc();

    }
    catch(Exception e){e.printStackTrace();}