Code: Select all
from dataclasses import dataclass
import random
@dataclass
class Point:
x: float
y: float
@dataclass
class Vector:
a: Point
b: Point
def x(self, y): return self.a.x + (y - self.a.y) * (self.b.x - self.a.x) / (self.b.y - self.a.y)
def y(self, x): return self.a.y + (x - self.a.x) * (self.b.y - self.a.y) / (self.b.x - self.a.x)
def process(v: Vector):
v.a.x = v.b.y
for i in range(10 ** 7):
process(Vector(Point(random.random(), random.random()), Point(random.random(), random.random())))
Code: Select all
ncalls tottime percall cumtime percall filename:lineno(function)
9/1 0.001 0.000 239.400 239.400 {built-in method builtins.exec}
1 130.752 130.752 239.400 239.400 test.py:1()
40000000 54.230 0.000 54.230 0.000 {method 'random' of '_random.Random' objects}
20000000 26.921 0.000 26.921 0.000 :2(__init__)
10000000 13.814 0.000 13.814 0.000 test.py:16(process)
Ganz allgemein: Wie würden Sie raten, die 2D-Vektor-/Matrixverarbeitung in Python möglichst leistungsfähig durchzuführen? Ich werde Tausende oder vielleicht Millionen einfacher algebraischer Berechnungen an 2D-Punkten durchführen, was in C nichts wäre, in Python jedoch einen Ansatz zu erfordern scheint.
Hinweis: Ich führe dies in einer VM aus, daher ist es etwas träge. Das Betriebssystem ist Ubuntu.
Mobile version