I want to use floating-point numbers in my J2ME (MIDP 2.0) program.
I don't need high precision, so I'm considering choosingfloat
type.
However, it seems to me that I read a Java article which says that double
is better supported by processors, so it is faster.
I'm doing many operations with such numbers, so performance is important to me.
The question is, which one of these data types works faster开发者_Go百科 in J2ME?
The performance difference between float and double is not that significant.
If you are doing those operation very frequently than float is better also it consume less space.
Update:
public long sumDouble(){
double[] arr = new double[1000];
for(int i = 0 ;i < 1000;i++){
arr[i]=new Random().nextFloat();
}
float sum = 0.0f;
long start = System.currentTimeMillis();
for(int i = 0 ;i < 1000;i++){
sum+=arr[i];
}
System.out.println(""+sum);
long end = System.currentTimeMillis();
return end - start;
}
public long sumFloat(){
double[] arr = new double[1000];
for(int i = 0 ;i < 1000;i++){
arr[i]=new Random().nextDouble();
}
double sum = 0.0f;
long start = System.currentTimeMillis();
for(int i = 0 ;i < 1000;i++){
sum+=arr[i];
}
System.out.println(""+sum);
long end = System.currentTimeMillis();
return end - start;
}
Try executing these methods in your real device to measure the performance difference Its not much significant.
精彩评论