18 March 2007

TAKE CARE ON TYPE CONVERSION --- STRING TO DOUBLE AND NUMBER TO DOUBLE

I consider about the cost of type conversion in Java.
And I test it with a 1 million call.And Understand that
if you well choose your type for conversion then you gain
from performance. :)

My Considirarion point is about converting to double.
Javas popular class String has flexibity and this flexibity makes
many developers to use it wherever possible.

But My Experiment shows that: Take Care On Scaleble Stuff ;)
I mean if you have the Number Class or sub types of it,
it will have dramatic effect over using String.

See my Code.
Timer Class for measuring Time.
Converter Class.
And Test Class.

Results On Intel(R) Pentium(R) M processor 2,00 Ghz
798 Mhz 1 Gb Ram PC

First Run:
----------------------------------------
Total Time Elapsed for STRING is : 375
Total Time Elapsed for int is : 32
Total Time Elapsed for float is : 15
Total Time Elapsed for NUMBER is : 16

Second Run
----------------------------------------
Total Time Elapsed for STRING is : 406
Total Time Elapsed for int is : 16
Total Time Elapsed for float is : 15
Total Time Elapsed for NUMBER is : 16

Third Run
------------------------------------------
Total Time Elapsed for STRING is : 406
Total Time Elapsed for int is : 31
Total Time Elapsed for float is : 31
Total Time Elapsed for NUMBER is : 32

Fourth Run
--------------------------------------------
Total Time Elapsed for STRING is : 329
Total Time Elapsed for int is : 31
Total Time Elapsed for float is : 15
Total Time Elapsed for NUMBER is : 16

Fifth Run
--------------------------------------------

Total Time Elapsed for STRING is : 359
Total Time Elapsed for int is : 16
Total Time Elapsed for float is : 16
Total Time Elapsed for NUMBER is : 15


Quick Result : If possible, You should prefer using number type or subtypes of it rather Than using String for conversion

No comments: