好吧,我最终使用了本机类的常用构造函数。它看起来对我来说绝对安全,并且从剩余的变体中最快。来自 Marshal::PtrToStructure() 评论的想法很好,但是对于我的测试示例,执行速度比使用构造函数的速度慢。指针转换是最快的解决方案,但是在评论中非常可怕的例子之后,我不会再冒险使用它了(除非我们真的需要优化它,那么 LayoutKind::Explicit 应该做的事情)。
这是我用于测试的代码:
// this is C++ native class
class NativePoint
{
public:
double x;
double y;
double z;
NativePoint()
{
}
NativePoint(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
}
NativePoint operator * (int value)
{
return NativePoint(x * value, y * value, z * value);
}
};
// this class managed C++ class
[StructLayout(LayoutKind::Sequential)]
public value class ManagedPoint
{
internal:
double x;
double y;
double z;
ManagedPoint(const NativePoint& p)
{
x = p.x;
y = p.y;
z = p.z;
}
ManagedPoint(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
}
public:
static ManagedPoint operator * (ManagedPoint a, double value)
{
return ManagedPoint((*reinterpret_cast<NativePoint*>(&(a))) * value);
}
ManagedPoint MultiplyBy(double value)
{
pin_ptr<ManagedPoint> pThisTmp = &*this;
NativePoint* pThis = reinterpret_cast<NativePoint*>(&*pThisTmp);
return ManagedPoint(*pThis * value);
}
};
// this class managed C++ class
[StructLayout(LayoutKind::Sequential)]
public value class ManagedPoint2
{
internal:
double x;
double y;
double z;
ManagedPoint2(const NativePoint& p)
{
x = p.x;
y = p.y;
z = p.z;
}
ManagedPoint2(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
}
public:
static ManagedPoint2 operator * (ManagedPoint2 a, double value)
{
return ManagedPoint2((NativePoint(a.x, a.y, a.z)) * value);
}
ManagedPoint2 MultiplyBy(double value)
{
return ManagedPoint2((NativePoint(this->x, this->y, this->z)) * value);
}
};
// this class managed C++ class
[StructLayout(LayoutKind::Sequential)]
public value class ManagedPoint3
{
internal:
double x;
double y;
double z;
ManagedPoint3(const NativePoint& p)
{
x = p.x;
y = p.y;
z = p.z;
}
ManagedPoint3(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
}
public:
static ManagedPoint3 operator * (ManagedPoint3 a, double value)
{
NativePoint p;
Marshal::StructureToPtr(a, IntPtr(&p), false);
return ManagedPoint3(p * value);
}
ManagedPoint3 MultiplyBy(double value)
{
NativePoint p;
Marshal::StructureToPtr(*this, IntPtr(&p), false);
return ManagedPoint3(p * value);
}
};
// this class managed C++ class
[StructLayout(LayoutKind::Sequential)]
public value class ManagedPoint4
{
internal:
double x;
double y;
double z;
ManagedPoint4(const NativePoint& p)
{
x = p.x;
y = p.y;
z = p.z;
}
ManagedPoint4(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
}
public:
static ManagedPoint4 operator * (ManagedPoint4 a, double value)
{
return ManagedPoint4(ManagedPoint4::ToNative(a) * value);
}
ManagedPoint4 MultiplyBy(double value)
{
return ManagedPoint4(ManagedPoint4::ToNative(*this) * value);
}
static NativePoint ToNative(const ManagedPoint4& pp)
{
NativePoint p;
Marshal::StructureToPtr(pp, IntPtr(&p), false);
return p;
}
};
// this should be called from C# code, or another .NET app
int main(array<System::String ^> ^args)
{
Stopwatch time;
time.Start();
for (int i = 0; i < 10000000; i++)
{
ManagedPoint a = ManagedPoint(1, 2, 3) * 4;
}
time.Stop();
Console::WriteLine("time: " + time.ElapsedMilliseconds);
Stopwatch time2;
time2.Start();
for (int i = 0; i < 10000000; i++)
{
ManagedPoint2 a2 = ManagedPoint2(1, 2, 3) * 4;
}
time2.Stop();
Console::WriteLine("time2: " + time2.ElapsedMilliseconds);
Stopwatch time3;
time3.Start();
for (int i = 0; i < 10000000; i++)
{
ManagedPoint3 a3 = ManagedPoint3(1, 2, 3) * 4;
}
time3.Stop();
Console::WriteLine("time3: " + time3.ElapsedMilliseconds);
Stopwatch time4;
time4.Start();
for (int i = 0; i < 10000000; i++)
{
ManagedPoint4 a3 = ManagedPoint4(1, 2, 3) * 4;
}
time4.Stop();
Console::WriteLine("time4: " + time4.ElapsedMilliseconds);
Console::ReadLine();
Console::WriteLine("======================================================");
Console::WriteLine();
return 0;
}
这是输出:
time: 374
time2: 382
time3: 857
time4: 961
time: 395
time2: 413
time3: 900
time4: 968
time: 376
time2: 378
time3: 840
time4: 909