Optimized algorithm to compare templates of two websites
Reading between the lines, here is what I propose. I assume that you are interested in the structure of websites, so the first step is to expose this structure.


Throw away all non-structure tags as well as attributes (maybe keep class) and text. 
 I imagine we would be left with &lt;div&gt;, &lt;table&gt;, &lt;ul&gt;/&lt;ol&gt;¹, HTML5 structure tags and little else, depending on coding style.
 For the sake of efficiency, use a SAX-style parser for this step.
Create a tree of the remaining tags, either directly or in a second run. 
 The structure of this tree should hold the information you want.
 You may want to cut everything that is nested deeper than some level;
 the overall structure of some website is likely to be clear from the first
 five to ten levels.
Compare the trees. What is useful here depends heavily on the kind of 
 similarity you want and on the degree of sophistication the used websites 
 display.

Equality of trees would be simplest measure but also coarse. There 
 has been work on tree similarity measures, see e.g. here. Some research may unearth a measure that suits your purpose.


Finetuning the steps will require experiments and will most likely depend on the class of websites you compare.




You say you remove lists and tables because the may contain different numbers of elements. While that is true, the action is overcompensating since the occurrence of a list or table can be significant. Instead, remove all contained tags but keep the main ones. But then again, maybe the templates differ in the amount of columns in some table?