2021-10 Py: switch to using collections.deque

By switching the lists I was using as stacks out with a deque, I
appear to have achieved a teeeensy speed improvement.

List `append()` and `pop()` methods have at most a O(n) time
complexity, where deques have a O(1) time complexity.

https://docs.python.org/3/library/collections.html#collections.deque

Signed-off-by: AKU <tom@tdpain.net>
This commit is contained in:
akp 2021-12-10 16:31:42 +00:00
parent c55c9839e2
commit a60facbf55
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
3 changed files with 9 additions and 8 deletions

View file

@ -3,12 +3,12 @@
"dir": "challenges/2021/10-syntaxScoring", "dir": "challenges/2021/10-syntaxScoring",
"implementations": { "implementations": {
"Python": { "Python": {
"part.1.avg": 0.0013953149318695068, "part.1.avg": 0.001248321533203125,
"part.1.max": 0.003077268600463867, "part.1.max": 0.004151821136474609,
"part.1.min": 0.0008635520935058594, "part.1.min": 0.0008554458618164062,
"part.2.avg": 0.0033985512256622316, "part.2.avg": 0.0030763609409332274,
"part.2.max": 0.0085296630859375, "part.2.max": 0.028047800064086914,
"part.2.min": 0.0021257400512695312 "part.2.min": 0.002079486846923828
} }
}, },
"numRuns": 1000 "numRuns": 1000

View file

@ -2,6 +2,7 @@ from typing import List, Optional, Tuple
from aocpy import BaseChallenge from aocpy import BaseChallenge
from dataclasses import dataclass from dataclasses import dataclass
import math import math
from collections import deque
CHECKER_POINTS = { CHECKER_POINTS = {
@ -24,7 +25,7 @@ class Chunk:
text: str text: str
def is_corrupted(self) -> Tuple[bool, Optional[str]]: def is_corrupted(self) -> Tuple[bool, Optional[str]]:
stack = [] stack = deque()
for char in self.text: for char in self.text:
if char == "(": if char == "(":
@ -45,7 +46,7 @@ class Chunk:
return False, None return False, None
def complete(self) -> str: def complete(self) -> str:
stack = [] stack = deque()
output = "" output = ""
n = 0 n = 0

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB

After

Width:  |  Height:  |  Size: 53 KiB

Before After
Before After