Skip to content

Instantly share code, notes, and snippets.

@magmax

magmax/tester.py Secret

Created May 28, 2024 05:13
Show Gist options
  • Select an option

  • Save magmax/1a938089dc277820bad239ebc77caecb to your computer and use it in GitHub Desktop.

Select an option

Save magmax/1a938089dc277820bad239ebc77caecb to your computer and use it in GitHub Desktop.
import timeit
def test():
for i in ("Original", "Namedtuple", "Tuples", "Logical", "Conditions", "ConditionsInline"):
for v1, v2, v3, v4 in [(100, 100, 100, 100), (100, 100, 100, 101), (100, 101, 100, 100), (101, 100, 100, 100), (100, 100, 101, 100), (100, 102, 100, 101), (100, 101, 100, 102)]:
result = timeit.timeit(
f"Timestamp({v1}, {v2}) > Timestamp({v3}, {v4})",
setup=f"from timestamp import Timestamp{i} as Timestamp",
number=1000000
)
yield i, v1, v2, v3, v4, result
stats = {}
for name, v1, v2, v3, v4, result in test():
if name not in stats:
stats[name] = []
print(f"testing {name} with ({v1}, {v2}) > ({v3}, {v4}) --> {result}")
stats[name].append(result)
averages = {}
for k, v in stats.items():
averages[k] = sum(v) / len(v)
better = min(averages.values())
for k, v in averages.items():
print(f"{k} {v} ({100 * (1 - (v/better))})%")
from typing import Dict, NamedTuple, Optional, Union
from collections import namedtuple
class TimestampNamedtuple(namedtuple("Timestamp", ["sec", "nsec"])):
"""A nanosecond-resolution timestamp."""
def __new__(cls, sec: float, nsec: float) -> None:
if nsec < 0 or nsec >= 1e9:
raise ValueError(f"Invalid value for nanoseconds in Timestamp: {nsec}")
if sec < 0:
nsec = -nsec
return super().__new__(cls, int(sec), int(nsec))
def __str__(self) -> str:
return f"{self.sec}.{self.nsec:09d}"
def __repr__(self) -> str:
return f"Timestamp({self.sec}, {self.nsec})"
def __float__(self) -> float:
return float(self.sec) + float(self.nsec) / 1e9
class TimestampTuples:
"""A nanosecond-resolution timestamp."""
def __init__(self, sec: float, nsec: float) -> None:
if nsec < 0 or nsec >= 1e9:
raise ValueError(f"Invalid value for nanoseconds in Timestamp: {nsec}")
if sec < 0:
nsec = -nsec
self.sec: int = int(sec)
self.nsec: int = int(nsec)
def __str__(self) -> str:
return f"{self.sec}.{self.nsec:09d}"
def __repr__(self) -> str:
return f"Timestamp({self.sec}, {self.nsec})"
def __float__(self) -> float:
return float(self.sec) + float(self.nsec) / 1e9
def __eq__(self, other: object) -> bool:
return isinstance(other, Timestamp) and self.sec == other.sec and self.nsec == other.nsec
def __ne__(self, other: object) -> bool:
return not self == other
def __gt__(self, other: "Timestamp") -> bool:
return (self.sec, self.nsec) > (other.sec, other.nsec)
def __lt__(self, other: "Timestamp") -> bool:
return (self.sec, self.nsec) < (other.sec, other.nsec)
class TimestampLogical:
"""A nanosecond-resolution timestamp."""
def __init__(self, sec: float, nsec: float) -> None:
if nsec < 0 or nsec >= 1e9:
raise ValueError(f"Invalid value for nanoseconds in Timestamp: {nsec}")
if sec < 0:
nsec = -nsec
self.sec: int = int(sec)
self.nsec: int = int(nsec)
def __str__(self) -> str:
return f"{self.sec}.{self.nsec:09d}"
def __repr__(self) -> str:
return f"Timestamp({self.sec}, {self.nsec})"
def __float__(self) -> float:
return float(self.sec) + float(self.nsec) / 1e9
def __eq__(self, other: object) -> bool:
return isinstance(other, Timestamp) and self.sec == other.sec and self.nsec == other.nsec
def __ne__(self, other: object) -> bool:
return not self == other
def __gt__(self, other: "Timestamp") -> bool:
return self.sec > other.sec or (self.sec == other.sec and self.nsec > other.nsec)
def __lt__(self, other: "Timestamp") -> bool:
return (self.sec, self.nsec) < (other.sec, other.nsec)
class TimestampConditions:
"""A nanosecond-resolution timestamp."""
def __init__(self, sec: float, nsec: float) -> None:
if nsec < 0 or nsec >= 1e9:
raise ValueError(f"Invalid value for nanoseconds in Timestamp: {nsec}")
if sec < 0:
nsec = -nsec
self.sec: int = int(sec)
self.nsec: int = int(nsec)
def __str__(self) -> str:
return f"{self.sec}.{self.nsec:09d}"
def __repr__(self) -> str:
return f"Timestamp({self.sec}, {self.nsec})"
def __float__(self) -> float:
return float(self.sec) + float(self.nsec) / 1e9
def __eq__(self, other: object) -> bool:
return isinstance(other, Timestamp) and self.sec == other.sec and self.nsec == other.nsec
def __ne__(self, other: object) -> bool:
return not self == other
def __gt__(self, other: "Timestamp") -> bool:
if self.sec == other.sec:
return self.nsec > other.nsec
return self.sec > other.sec
def __lt__(self, other: "Timestamp") -> bool:
return (self.sec, self.nsec) < (other.sec, other.nsec)
class TimestampConditionsInline:
"""A nanosecond-resolution timestamp."""
def __init__(self, sec: float, nsec: float) -> None:
if nsec < 0 or nsec >= 1e9:
raise ValueError(f"Invalid value for nanoseconds in Timestamp: {nsec}")
if sec < 0:
nsec = -nsec
self.sec: int = int(sec)
self.nsec: int = int(nsec)
def __str__(self) -> str:
return f"{self.sec}.{self.nsec:09d}"
def __repr__(self) -> str:
return f"Timestamp({self.sec}, {self.nsec})"
def __float__(self) -> float:
return float(self.sec) + float(self.nsec) / 1e9
def __eq__(self, other: object) -> bool:
return isinstance(other, Timestamp) and self.sec == other.sec and self.nsec == other.nsec
def __ne__(self, other: object) -> bool:
return not self == other
def __gt__(self, other: "Timestamp") -> bool:
return self.nsec > other.nsec if self.sec == other.sec else self.sec > other.sec
def __lt__(self, other: "Timestamp") -> bool:
return (self.sec, self.nsec) < (other.sec, other.nsec)
class TimestampOriginal:
"""A nanosecond-resolution timestamp."""
def __init__(self, sec: float, nsec: float) -> None:
if nsec < 0 or nsec >= 1e9:
raise ValueError(f"Invalid value for nanoseconds in Timestamp: {nsec}")
if sec < 0:
nsec = -nsec
self.sec: int = int(sec)
self.nsec: int = int(nsec)
def __str__(self) -> str:
return f"{self.sec}.{self.nsec:09d}"
def __repr__(self) -> str:
return f"Timestamp({self.sec}, {self.nsec})"
def __float__(self) -> float:
return float(self.sec) + float(self.nsec) / 1e9
def __eq__(self, other: object) -> bool:
return isinstance(other, Timestamp) and self.sec == other.sec and self.nsec == other.nsec
def __ne__(self, other: object) -> bool:
return not self == other
def __gt__(self, other: "Timestamp") -> bool:
return self.sec > other.sec or self.nsec > other.nsec
def __lt__(self, other: "Timestamp") -> bool:
return (self.sec, self.nsec) < (other.sec, other.nsec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment