Created
December 10, 2024 15:50
-
-
Save katzefudder/f70ffdf8eebd9f2d6ae1b111947bdd21 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import xml.sax | |
from xml.dom import minidom | |
# SAX Parser Handler | |
class MySAXHandler(xml.sax.ContentHandler): | |
def __init__(self): | |
self.current_tag = None | |
def startElement(self, tag, attributes): | |
self.current_tag = tag | |
def characters(self, content): | |
if self.current_tag and content.strip(): | |
pass # Process characters if needed | |
def endElement(self, tag): | |
self.current_tag = None | |
def test_sax(): | |
parser = xml.sax.make_parser() | |
handler = MySAXHandler() | |
parser.setContentHandler(handler) | |
with open("test.xml", "r") as f: | |
parser.parse(f) | |
def test_dom(): | |
with open("test.xml", "r") as f: | |
minidom.parse(f) | |
# Measure time for SAX parsing | |
start_time = time.time() | |
test_sax() | |
sax_time = time.time() - start_time | |
print(f"SAX parsing time: {sax_time:.6f} seconds") | |
# Measure time for DOM parsing | |
start_time = time.time() | |
test_dom() | |
dom_time = time.time() - start_time | |
print(f"DOM parsing time: {dom_time:.6f} seconds") |
This file has been truncated, but you can view the full file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SAX parsing time: 0.098901 seconds
DOM parsing time: 0.402619 seconds
on my Apple M1 Max