Hi Daniel,

> Whenever you have the time to
> whittle it down
> to the bare essentials, please post again.

Here is a reduced, standalone test class,
Performance.java.

/*
 * Performance.java
 *
 * Created on 15 October 2001, 22:07
 */


import java.util.* ;

import org.apache.oro.text.regex.* ;


/**
 *
 * @author  jdb
 * @version v 1.0
 */
public class Performance {
    
    
    private static final String
SERVER_ACTIVE_HTML_PATTERN =
    /*
     * <!-- name="news" -->
     * or
     * <!-- name="news" template="news-tmpl" -->
     *
     * (Remember to escape backslash
     *
     * \n -> \\n
     * \w -> \\w
     *
     * etc)
     *
     */
   
"<!--\\s*name\\s*=\\s*\"([\\w\\-]+)\"\\s*(template\\s*=\\s*\"[\\w\\-]+\")?\\s*-->"
+
    
    /*
     * enclosed content
     */
    "((\\s|.)*)" +
    
    /*
     * <!-- name="/news" -->
     */
    "<!--\\s*name\\s*=\\s*\"/\\1\"\\s*-->" ;
    
    
    static protected PatternMatcher _matcher ;
    static protected Pattern _pattern ;
    static protected MalformedPatternException
_patternException ;
    
    
    /* Static initialiser */
    {
        
        _matcher  = new Perl5Matcher () ;
        
        PatternCompiler compiler = new Perl5Compiler
();
        
        try {
            _pattern = compiler.compile (
            SERVER_ACTIVE_HTML_PATTERN,
            Perl5Compiler.CASE_INSENSITIVE_MASK |
            Perl5Compiler.READ_ONLY_MASK );
        }
        catch(MalformedPatternException e) {
            /* how can an exception be thrown from a
static initialiser? */
            _pattern = null ;
            _patternException = e ;
        }
    }
    
    
    /**
     * Parse the HTML fragment.
     *
     * Match comment pairs in psuedo XML format:
     *
     * <!-- name="xxx" -->
     * ...
     * <!-- name="/xxx" -->
     *
     * or
     *
     * <!-- name="xxx" template="yyy" -->
     * ...
     * <!-- name="/xxx" -->
     *
     *
     *
     *
     * @param html The HTML to search in.
     */
    protected void matchComments ( String htmlString )
throws Exception {
        
        /* delayed exception from static initialiser
*/
        if ( _pattern == null ) {
            throw new Exception (
_patternException.toString () );
        }
        
        MatchResult result;
        
        PatternMatcherInput input = new
PatternMatcherInput ( htmlString );
        
        int i = 1 ;
        boolean matched ;
        while( true ) {
            
            long t = System.currentTimeMillis () ;
            
            matched = _matcher.contains (input,
_pattern) ;
            
            System.out.println ( "           #" + i +
": " +
            ( ( System.currentTimeMillis () - t ) /
1000 )
            );
            
            if ( ! matched )
                break ;
            
            result = _matcher.getMatch ();
            
            i++ ;
        }
        
    }
    
    
    public static void main (String[] args) {
        (new Performance ()).match () ;
    }
    
    
    protected void match () {
        try {
            testBinarySequences () ;
        }
        catch ( Exception e ) {
            e.printStackTrace () ;
        }
    }
    
    
    /**
     * Test many sequences.
     *
     * A = the pattern we are looking for
     * H = other HTML
     *
     */
    public void testBinarySequences () throws
Exception {
        
       /*
        * (HH = H)
        */
        
        List comboList = Arrays.asList ( new String []
{
            /* 1 */
            "H", "A",
            /* 2 */
            "AH", "AA", "HA",
            /* 3 */
            "AAH", "AAA", "AHA", "HAH", "HAA",
            /* 4 */
            "AAAH", "AAAA", "AAHA", "AHAH", "AHAA",
"HAAH", "HAAA", "HAHA",
            /* 5 */
            "AAAAH", "AAAAA", "AAAHA", "AAHAH",
"AAHAA", "AHAAH", "AHAAA", "AHAHA",
            "HAAAH", "HAAAA", "HAAHA", "HAHAH",
"HAHAA"
            
        } ) ;
        
        String html = "<b><i>Hi</i></b>\n" ;
        String SAHOpen = "<!-- name=\"" ;
        String SAHClose = "\" -->" ;
        String SAHContent = "<!-- element=\"content\"
-->Hi<!-- element=\"/content\" -->\n";
        
        int namePostfix = 0 ;
        
        Iterator it = comboList.iterator () ;
        while ( it.hasNext () ) {
            
            String seq = (String) it.next () ;
            
            StringBuffer sb = new StringBuffer () ;
            
            for ( int i = 0; i < seq.length () ; i++,
namePostfix++ ) {
                
                if ( seq.charAt ( i ) == 'A' ) {
                    sb.append (
                    SAHOpen + "name-" + namePostfix +
SAHClose +
                    SAHContent +
                    SAHOpen + "/name-" + namePostfix +
SAHClose
                    ) ;
                }
                else {
                    sb.append ( html ) ;
                }
            }
            System.out.println ("input: " + seq);
            System.out.println ( "(Seconds for each
contains() invocation.)" );
            
            long t = System.currentTimeMillis () ;
            
            matchComments ( sb.toString () ) ;
            
            System.out.println ( "Total seconds: " +
            ( ( System.currentTimeMillis () - t ) /
1000 )
            );
            
            System.out.println ("");
        }
    }
    
    
}

/*
 These were the parse times.
 
(Seconds for each contains() invocation.)
           #1: 0
Total seconds: 0
 
input: A
(Seconds for each contains() invocation.)
           #1: 0
           #2: 0
Total seconds: 0
 
input: AH
(Seconds for each contains() invocation.)
           #1: 0
           #2: 0
Total seconds: 0
 
input: AA
(Seconds for each contains() invocation.)
           #1: 0
           #2: 0
           #3: 0
Total seconds: 0
 
input: HA
(Seconds for each contains() invocation.)
           #1: 0
           #2: 0
Total seconds: 0
 
input: AAH
(Seconds for each contains() invocation.)
           #1: 0
           #2: 0
           #3: 0
Total seconds: 0
 
input: AAA
(Seconds for each contains() invocation.)
           #1: 13
           #2: 0
           #3: 0
           #4: 0
Total seconds: 13
 
input: AHA
(Seconds for each contains() invocation.)
           #1: 0
           #2: 0
           #3: 0
Total seconds: 0
 
input: HAH
(Seconds for each contains() invocation.)
           #1: 0
           #2: 0
Total seconds: 0
 
input: HAA
(Seconds for each contains() invocation.)
           #1: 0
           #2: 0
           #3: 0
Total seconds: 0
 
input: AAAH
(Seconds for each contains() invocation.)
           #1: 24
           #2: 0
           #3: 0
           #4: 0
Total seconds: 24
 
input: AAAA
(Seconds for each contains() invocation.)
--- killed process ---
 */


____________________________________________________________
Do You Yahoo!?
Get your free @yahoo.co.uk address at http://mail.yahoo.co.uk
or your free @yahoo.ie address at http://mail.yahoo.ie

Reply via email to