gpt4 book ai didi

c# - C#两个几乎相同的线程,性能差异很大

转载 作者:行者123 更新时间:2023-12-03 13:22:59 25 4
gpt4 key购买 nike

我在C#中拥有一个大型的多线程系统,我意识到两个线程之间的性能差异很大。现在,我设计了两个几乎完全相同的线程,其中一个的执行速度快4-5倍(如果您更改它们必须运行的循环数量,它会线性扩展)。
和不同吗?一种笨拙的条件围绕其中之一的实际重码。这对我来说毫无意义,而且如果这么小的细节可能产生如此巨大的影响,我将无能为力地进行优化。这已在Unity中进行了测试,因此在其他环境中结果可能会有所不同。
线程完成时间:2.8秒。
ThreadB完成时间:0.6秒。

请注意,ThreadB是有条件的(在第一次迭代时,它将立即评估为true)。
如此愚蠢的代码添加如何使实际有效负载(for循环和数字运算)的执行速度如此之快?另外,如果我在ThreadB的情况下直接将“delay”变量更改为静态“0.0”,则其性能再次类似于ThreadA。换句话说:一个单精度 double 型,无论是硬编码值还是引用变量,都会使性能差异提高4-5倍。
不用管实际的算法,这只会使计算机处理一些数字。我知道我要一次又一次地比较相同的数据,这是不重要的。
我不是编译专家,也没有办法探讨实际的计算机/汇编代码中的差异。我只知道两者之间的差异是巨大的,而且对我来说简直是无稽之谈。我想念什么?我偶然发现了这一点,将来我可能无法知道给定线程以20%的可能速度执行,而只需稍作更改便可以解决该问题。
请。我需要一个 Nerd 才能使这一切从纯粹的魔法变成“哦,这就是为什么...!现在我知道将来如何避免它了……”。我知道C#的编译被托管的东西层包围,但是一定有逻辑上的原因。正确的?
这是一些具有一些简单结构的测试代码来支持它。如果有人有时间检查他们是否获得与我相同的结果,我会很高兴。

using System.Threading;

public class ThreadTest
{
Thread threadA;
Thread threadB;

bool runThreadA = false;
bool runThreadB = false;

System.Diagnostics.Stopwatch stopWatch;

double elapsedTimeA = 0;
double elapsedTimeB = 0;

public ThreadTest()
{
stopWatch = new System.Diagnostics.Stopwatch();
StartThreads();
}

public void StartThreads ()
{
stopWatch.Reset();
stopWatch.Start();
threadA = new Thread(ThreadA);
threadB = new Thread(ThreadB);

runThreadA = true;
runThreadB = true;

elapsedTimeA = 0;
elapsedTimeB = 0;
threadA.Start();
threadB.Start();
}

void ThreadA ()
{
while (runThreadA)
{

runThreadA = false;
double preTicks = stopWatch.ElapsedTicks;

Line3Double lineA = new Line3Double(new Vector3DoublePrecision(10, 20, 30), new Vector3DoublePrecision(100, 140, 180));
Line3Double lineB = new Line3Double(new Vector3DoublePrecision(-10, -20, -30), new Vector3DoublePrecision(-100, -140, -180));

int lines = 1000;

for (int i = 0; i < 8; i++)
{
for (int j = 0; j < lines; j++)
{
double aStartX = lineA.startX;
double aStartY = lineA.startY;
double aStartZ = lineA.startZ;

double aEndX = lineA.endX;
double aEndY = lineA.endY;
double aEndZ = lineA.endZ;

double aDirX = lineA.dirX;
double aDirY = lineA.dirY;
double aDirZ = lineA.dirZ;

double aDotSelf = lineA.dotSelf;

for (int k = 0; k < 8; k++)
{
for (int l = 0; l < lines; l++)
{
double wX = aStartX - lineB.startX;
double wY = aStartY - lineB.startY;
double wZ = aStartZ - lineB.startZ;

double b = aDirX * lineB.dirX + aDirY * lineB.dirY + aDirZ * lineB.dirZ;
double d = aDirX * wX + aDirY * wY + aDirZ * wZ;
double e = lineB.dirX * wX + lineB.dirY * wY + lineB.dirZ * wZ;

double D = aDotSelf * lineB.dotSelf - b * b;
double sc, tc;
if (D < 0.0000001)
{
sc = 0.0f;
tc = (b > lineB.dotSelf ? d / b : e / lineB.dotSelf);
}
else
{
sc = (b * e - lineB.dotSelf * d) / D;
tc = (aDotSelf * e - b * d) / D;
}

double shortestX = wX + (sc * aDirX) - (tc * lineB.dirX);
double shortestY = wY + (sc * aDirY) - (tc * lineB.dirY);
double shortestZ = wZ + (sc * aDirZ) - (tc * lineB.dirZ);

double distance = shortestX * shortestX + shortestY * shortestY + shortestZ * shortestZ;
}
}
}
}

double postTicks = stopWatch.ElapsedTicks;
double time = ((postTicks - preTicks) / System.Diagnostics.Stopwatch.Frequency) * 1000;
elapsedTimeA = time;
}
}

void ThreadB()
{
long startTicks = stopWatch.ElapsedTicks;
double delay = 0;

while (runThreadB)
{
if ((double)(stopWatch.ElapsedTicks - startTicks) / System.Diagnostics.Stopwatch.Frequency >= delay)
{
runThreadB = false;
double preTicks = stopWatch.ElapsedTicks;

Line3Double lineA = new Line3Double(new Vector3DoublePrecision(10, 20, 30), new Vector3DoublePrecision(100, 140, 180));
Line3Double lineB = new Line3Double(new Vector3DoublePrecision(-10, -20, -30), new Vector3DoublePrecision(-100, -140, -180));

int lines = 1000;

for (int i = 0; i < 8; i++)
{
for (int j = 0; j < lines; j++)
{
double aStartX = lineA.startX;
double aStartY = lineA.startY;
double aStartZ = lineA.startZ;

double aEndX = lineA.endX;
double aEndY = lineA.endY;
double aEndZ = lineA.endZ;

double aDirX = lineA.dirX;
double aDirY = lineA.dirY;
double aDirZ = lineA.dirZ;

double aDotSelf = lineA.dotSelf;

for (int k = 0; k < 8; k++)
{
for (int l = 0; l < lines; l++)
{
double wX = aStartX - lineB.startX;
double wY = aStartY - lineB.startY;
double wZ = aStartZ - lineB.startZ;

double b = aDirX * lineB.dirX + aDirY * lineB.dirY + aDirZ * lineB.dirZ;
double d = aDirX * wX + aDirY * wY + aDirZ * wZ;
double e = lineB.dirX * wX + lineB.dirY * wY + lineB.dirZ * wZ;

double D = aDotSelf * lineB.dotSelf - b * b;
double sc, tc;
if (D < 0.0000001)
{
sc = 0.0f;
tc = (b > lineB.dotSelf ? d / b : e / lineB.dotSelf);
}
else
{
sc = (b * e - lineB.dotSelf * d) / D;
tc = (aDotSelf * e - b * d) / D;
}

double shortestX = wX + (sc * aDirX) - (tc * lineB.dirX);
double shortestY = wY + (sc * aDirY) - (tc * lineB.dirY);
double shortestZ = wZ + (sc * aDirZ) - (tc * lineB.dirZ);

double distance = shortestX * shortestX + shortestY * shortestY + shortestZ * shortestZ;
}
}
}
}

double postTicks = stopWatch.ElapsedTicks;
double time = ((postTicks - preTicks) / System.Diagnostics.Stopwatch.Frequency) * 1000;
elapsedTimeB = time;
}
}
}
}
public struct Vector3DoublePrecision
{
public double x;
public double y;
public double z;

public Vector3DoublePrecision(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
}

public struct Line3Double
{
public double startX;
public double startY;
public double startZ;
public double endX;
public double endY;
public double endZ;

public double dirX;
public double dirY;
public double dirZ;

public double dotSelf;

public Line3Double(Vector3DoublePrecision start, Vector3DoublePrecision end)
{
startX = start.x;
startY = start.y;
startZ = start.z;

endX = end.x;
endY = end.y;
endZ = end.z;

dirX = end.x - start.x;
dirY = end.y - start.y;
dirZ = end.z - start.z;

dotSelf = dirX * dirX + dirY * dirY + dirZ * dirZ;
}
}

最佳答案

解决了...是Unity特定的编译问题。将脚本后端从Mono更改为IL2CPP,不仅使所有功能都表现得更好,而且还消除了上述问题。因此,应归咎于JIT编译。仍然不确定如此笨拙的条件如何使我的线程比没有线程更好地执行,但是现在它可以工作了。通过修复,我学到了很多东西。

关于c# - C#两个几乎相同的线程,性能差异很大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64985681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com