Skip to content

Commit c94f869

Browse files
committed
optimize the parsing code for speed
1 parent c660975 commit c94f869

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/pylhe/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -640,28 +640,28 @@ def _fromcontext(
640640
with_attributes: bool,
641641
) -> Iterator["LHEEvent"]:
642642
index_map = _get_index_to_id_map(lheinit) if with_attributes else {}
643-
for event, element in context:
643+
644+
for idx, (event, element) in enumerate(context, 1):
644645
if event == "end" and element.tag == "event":
645646
if element.text is None:
646647
err = "<event> block has no text."
647648
raise ValueError(err)
648649

649-
data = element.text.strip().split("\n")
650+
data = element.text.splitlines()
650651
eventdata_str, particles_str = data[0], data[1:]
652+
particles_str = [p.strip() for p in particles_str]
651653

652654
eventinfo = LHEEventInfo.fromstring(eventdata_str)
653655
particles = [
654656
LHEParticle.fromstring(p)
655657
for p in particles_str
656-
if not p.strip().startswith("#")
658+
if not p.startswith("#")
657659
]
658660

659661
if with_attributes:
660662
weights = {}
661663
attrib = element.attrib
662-
optional = [
663-
p.strip() for p in particles_str if p.strip().startswith("#")
664-
]
664+
optional = [p for p in particles_str if p.startswith("#")]
665665

666666
for sub in element:
667667
if sub.tag == "weights":
@@ -691,7 +691,9 @@ def _fromcontext(
691691

692692
# Clear memory
693693
element.clear()
694-
root.clear()
694+
# Clear the root every 100 elements
695+
if idx % 100 == 0:
696+
root.clear()
695697

696698
@property
697699
def graph(self) -> graphviz.Digraph:

0 commit comments

Comments
 (0)