Was hat :2(__init__) mit mathematischen Berechnungen zu tun?Python

Python-Programme
Anonymous
 Was hat :2(__init__) mit mathematischen Berechnungen zu tun?

Post by Anonymous »

Ich bin gerade dabei, einige mathematische Operationen zu schreiben. Hier ist ein Beispielprogramm, um Ihnen eine Vorstellung davon zu geben, was ich tun könnte:

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())))
Es läuft nicht so schnell, wie ich es gerne hätte, also profiliere ich das Skript mit python3 -m cProfile test.py, hier sind die ersten paar Zeilen der Ausgabe:

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)
Was macht :2(__init__) dort und warum nimmt es so viel Zeit in Anspruch? Nirgendwo in meinem Programm habe ich Strings erwähnt.
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.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post