FileDocCategorySizeDatePackage
FilterComparison2.javaAPI DocExample8878Fri May 10 10:18:46 BST 2002tuning.string

FilterComparison2

public class FilterComparison2 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)

    MatchReader2.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 voidfilter3(java.lang.String filter, java.lang.String filename, boolean print)

    count = 0;
    Pattern p = Pattern.compile("^.*" + filter + ".*$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
    CharBuffer cbuff = CharBuffer.wrap(buf);
    Matcher m = p.matcher(cbuff);
    while( m.find() )
    {
      count++;
      if (print)
        System.out.println(m.group());
    }
    System.out.println(count + " lines matched.");
  
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();
      filter3(args[0], args[1], false);
      time = System.currentTimeMillis() - time;
      System.out.println("filter3 time (millis) = " + time);

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

      time = System.currentTimeMillis();
      filter1(args[0], args[1], false);
      time = System.currentTimeMillis() - time;
      System.out.println("filter1 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("filter2 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();}