扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
using namespace System;
#define ARRAY_SIZE 1000
struct bubbleBase
{
int value;
};
class bubble1:public bubbleBase
{
public:
virtual int getvalue(){return value;}
virtual void setvalue(int newvalue){value=newvalue;}
};
class bubble2:public bubbleBase
{
public:
virtual int __clrcall getvalue(){return value;}
virtual void __clrcall setvalue(int newvalue){value=newvalue;}
};
template<class T>
void bubbleSort(int length)
{
TimeSpan ts;
T* array1=new T[ARRAY_SIZE];
for (int i=0;i<ARRAY_SIZE ;i++)
{
array1[i].setvalue(ARRAY_SIZE-i-1);
}
Int64 ticks=DateTime::Now.Ticks;
int i, j,temp, test;
for(i = length - 1; i > 0; i--)
{
test=0;
for(j = 0; j < i; j++)
{
if(array1[j].getvalue() > array1[j+1].getvalue())
{
temp = array1[j].getvalue();
array1[j].setvalue(array1[j+1].getvalue());
array1[j+1] .setvalue(temp);
test=1;
}
}
if(test==0) break;
}
ts=TimeSpan::FromTicks(DateTime::Now.Ticks-ticks);
Console::WriteLine("BubbleSort {0} Items: {1} Ticks",
ARRAY_SIZE,
ts.Ticks );
delete array1;
}
int main(array<System::String ^> ^args)
{
bubbleSort<bubble1>(ARRAY_SIZE);
bubbleSort<bubble2>(ARRAY_SIZE);
return 0;
}
|
运行结果是
BubbleSort 1000 Items: 3281250 Ticks
BubbleSort 1000 Items: 312500 Ticks
可以看到,__clrcall会大大加快在托管代码中调用托管函数的速度。
顺便说一下,在随VC8.0发布的STL中增加了很多安全特性,但是这也会造成程序的运行速度减慢。如果你确认程序不会有缓冲区溢出或者内存越界访问的问题,那么可以把_SECURE_SCL定义成0来关掉这个特性。
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。