2009年4月14日 星期二

computer speed of arithmetic operations


/**
根據speed.java四則運算時間測試結果,

整數乘/除餘運算時間約是整數加減運算時間的5/10倍以上
實數部份比較特別,乘和加減時間幾乎一樣,除法為加減法近200倍,求餘數為加減法近10倍以上.

C:\java>java speed ia is im id ip la ls lm ld lp fa fs fm fd fp da ds dm dd dp
The ia time is 438 ms, or 4.38000e-06 ms per ia
The is time is 422 ms, or 4.22000e-06 ms per is
The im time is 1906 ms, or 1.90600e-05 ms per im
The id time is 4938 ms, or 4.93800e-05 ms per id
The ip time is 5546 ms, or 5.54600e-05 ms per ip
The la time is 453 ms, or 4.53000e-06 ms per la
The ls time is 453 ms, or 4.53000e-06 ms per ls
The lm time is 2438 ms, or 2.43800e-05 ms per lm
The ld time is 7750 ms, or 7.75000e-05 ms per ld
The lp time is 7391 ms, or 7.39100e-05 ms per lp
The fa time is 328 ms, or 3.28000e-06 ms per fa
The fs time is 329 ms, or 3.29000e-06 ms per fs
The fm time is 328 ms, or 3.28000e-06 ms per fm
The fd time is 57422 ms, or 0.000574220 ms per fd
The fp time is 4860 ms, or 4.86000e-05 ms per fp
The da time is 328 ms, or 3.28000e-06 ms per da
The ds time is 328 ms, or 3.28000e-06 ms per ds
The dm time is 328 ms, or 3.28000e-06 ms per dm
The dd time is 66000 ms, or 0.000660000 ms per dd
The dp time is 4953 ms, or 4.95300e-05 ms per dp
*/
public class speed
{

public static long operation(String op, long loop)
{
int i=500, i2=100, i1=50;
long l=5000,l2=1000, l1=500;
float f=1e3f, f2=1.01f, f1=2e3f;
double d=1e6, d2=1.01, d1=2e2;
long k;

//int loop=(int) 1e6;
long start = System.currentTimeMillis();

if(op.equals("ia")) for(k=1; k<=loop; k++) i=i+i2;
else if(op.equals("is")) for(k=1; k<=loop; k++) i=i-i2;
else if(op.equals("im")) for(k=1; k<=loop; k++) i=i*i2;
else if(op.equals("id")) for(k=1; k<=loop; k++) i=i/i2;
else if(op.equals("ip")) for(k=1; k<=loop; k++) i=i%i2;

else if(op.equals("la")) for(k=1; k<=loop; k++) l=l+l2;
else if(op.equals("ls")) for(k=1; k<=loop; k++) l=l-l2;
else if(op.equals("lm")) for(k=1; k<=loop; k++) l=l*l2;
else if(op.equals("ld")) for(k=1; k<=loop; k++) l=l/l2;
else if(op.equals("lp")) for(k=1; k<=loop; k++) l=l%l2;

else if(op.equals("fa")) for(k=1; k<=loop; k++) f=f+f2;
else if(op.equals("fs")) for(k=1; k<=loop; k++) f=f-f2;
else if(op.equals("fm")) for(k=1; k<=loop; k++) f=f*f2;
else if(op.equals("fd")) for(k=1; k<=loop; k++) f=f/f2;
else if(op.equals("fp")) for(k=1; k<=loop; k++) f=f%f2;

else if(op.equals("da")) for(k=1; k<=loop; k++) d=d+d2;
else if(op.equals("ds")) for(k=1; k<=loop; k++) d=d-d2;
else if(op.equals("dm")) for(k=1; k<=loop; k++) d=d*d2;
else if(op.equals("dd")) for(k=1; k<=loop; k++) d=d/d2;
else if(op.equals("dp")) for(k=1; k<=loop; k++) d=d%d2;

long end = System.currentTimeMillis();
long time = end - start;

return time;
}

public static void main(String args[])
{
if(args.length==0)
{
System.err.println("java speed [i|l|f|d][a|s|m|d|p]");
System.exit(0);
}

for(int a=0; a < args.length; a++)
{
String op = args[a];

long loop = (long) 1e8;
long time = operation(op,loop);
System.err.printf("The %s time is %d ms, or %g ms per %s\n",
op, time, (float) time / loop, op);
}
}
}

沒有留言: