原生程式碼與託管程式碼的一個簡單效能對比
阿新 • • 發佈:2019-02-11
using System;
staticclass Program
{
staticint RAND_MAX =0x7fff;
class CMyRand
{
privatelong _my_holdrand=1;
publiclong Next()
{
long result=_my_holdrand *214013+2531011;
_my_holdrand =result;
return ( (result>>16) & RAND_MAX );
}
};
static CMyRand random=new CMyRand();
staticint NPARTS =1000;
staticint NITER =201;
staticint DIMS =3;
staticdouble pot;
staticdouble distx, disty, distz, dist;
staticdouble[] r =newdouble[DIMS*NPARTS];
staticint CLOCKS_PER_SEC =1000;
staticvoid Main()
{
int i;
int start, stop;
initPositions();
updatePositions();
start = Environment.TickCount;
for (i =0; i < NITER; i++)
{
pot =0.0;
computePot();
if (i %10==0) Console.WriteLine("{0}: Potential: {1:##########.#######}", i, pot);
updatePositions();
}
stop = Environment.TickCount;
Console.WriteLine("Seconds = {0:##########.#########}", (double)(stop - start) / CLOCKS_PER_SEC);
Console.ReadLine();
}
staticvoid initPositions()
{
for (int i =0; i < DIMS; i++)
for (int j =0; j < NPARTS; j++)
r[i + j * DIMS] =0.5+ (random.Next() * (1.0/ RAND_MAX));
}
staticvoid updatePositions()
{
for (int i =0; i < DIMS; i++)
for (int j =0; j < NPARTS; j++)
r[i + j * DIMS] -=0.5+ (random.Next() * (1.0/ RAND_MAX));
}
staticint computePot()
{
for (int i =0; i < NPARTS; i++)
{
for (int j =0; j < i -1; j++)
{
distx = (r[0+ j * DIMS] - r[0+ i * DIMS]) * (r[0+ j * DIMS] - r[0+ i * DIMS]);
disty = (r[1+ j * DIMS] - r[1+ i * DIMS]) * (r[1+ j * DIMS] - r[1+ i * DIMS]);
distz = (r[2+ j * DIMS] - r[2+ i * DIMS]) * (r[2+ j * DIMS] - r[2+ i * DIMS]);
dist = Math.Sqrt(distx + disty + distz);
pot +=1.0/ dist;
}
}
return0;
}
}
staticclass Program
{
staticint RAND_MAX =0x7fff;
class CMyRand
{
privatelong _my_holdrand=1;
publiclong Next()
{
long result=_my_holdrand *214013+2531011;
_my_holdrand =result;
return ( (result>>16) & RAND_MAX );
}
};
staticint NPARTS =1000;
staticint NITER =201;
staticint DIMS =3;
staticdouble pot;
staticdouble distx, disty, distz, dist;
staticdouble[] r =newdouble[DIMS*NPARTS];
staticint CLOCKS_PER_SEC =1000;
staticvoid Main()
{
int i;
initPositions();
updatePositions();
start = Environment.TickCount;
for (i =0; i < NITER; i++)
{
pot =0.0;
computePot();
if (i %10==0) Console.WriteLine("{0}: Potential: {1:##########.#######}", i, pot);
updatePositions();
}
stop
Console.WriteLine("Seconds = {0:##########.#########}", (double)(stop - start) / CLOCKS_PER_SEC);
Console.ReadLine();
}
staticvoid initPositions()
{
for (int i =0; i < DIMS; i++)
for (int j =0; j < NPARTS; j++)
r[i + j * DIMS] =0.5+ (random.Next() * (1.0/ RAND_MAX));
}
staticvoid updatePositions()
{
for (int i =0; i < DIMS; i++)
for (int j =0; j < NPARTS; j++)
r[i + j * DIMS] -=0.5+ (random.Next() * (1.0/ RAND_MAX));
}
staticint computePot()
{
for (int i =0; i < NPARTS; i++)
{
for (int j =0; j < i -1; j++)
{
distx = (r[0+ j * DIMS] - r[0+ i * DIMS]) * (r[0+ j * DIMS] - r[0+ i * DIMS]);
disty = (r[1+ j * DIMS] - r[1+ i * DIMS]) * (r[1+ j * DIMS] - r[1+ i * DIMS]);
distz = (r[2+ j * DIMS] - r[2+ i * DIMS]) * (r[2+ j * DIMS] - r[2+ i * DIMS]);
dist = Math.Sqrt(distx + disty + distz);
pot +=1.0/ dist;
}
}
return0;
}
}