Something I wanted to pass along - no bug, no fix, just interesting fact...

In the NumberKey code is the following

    /**
     * Creates a NumberKey equivalent to <code>key</code>.
     * Convenience only. Not very efficient at all.
     */
    public NumberKey(int key)
    {
        this.key = new BigDecimal(new Integer(key).toString());
    }

The "not very efficient at all" comment caught my eye, so I thought I would
try to speed it up. 
I too thought the int->Integer->String->BigDecimal conversion should be a
problem.

I found that it is not at all, in fact the very opposite! I wont waste
everyones time with
all my experiments, but suffice it to say that this is actually the FASTEST
way to create
a BigDecimal from a numeric source. In fact NumberKey(int) is more than
twice as fast as
NumberKey(long)!
My final test ran 1,000,000 iterations of creating
a NumberKey from an integer, then 1,000,000 iterations of creating a
NumberKey from 
a long. Here are the results.

1,000,000 NumberKey(int)  =  875.0 ms. Time per iteration = 0.000875 ms
1,000,000 NumberKey(long) = 1812.0 ms. Time per iteration = 0.001812 ms
Difference per iteration is 0.000937 ms

for those interested, here is the test code


    public static void main(String[] args) {
        int intval = 1;
        long longval = 1;
        double start =0;
        // make sure classloader has NumberKey loaded - this made a
difference!
        NumberKey z = new NumberKey(1); 
        start = ((double)System.currentTimeMillis());
        for(int i = 0 ; i < 1000000 ; i++){
            NumberKey n = new NumberKey(longval);
        }
        double et = ((double)System.currentTimeMillis())-start;
        double itD = et/1000000;
        System.out.println("Elapsed Time, long cvt = "+et+" time per
iteration = "+itD);
        start = System.currentTimeMillis();
        for(int i = 0;i<1000000;i++){
            NumberKey n = new NumberKey(intval);
        }
        et = ((double)System.currentTimeMillis())-start;
        double itI = et/1000000;
        System.out.println("Elapsed Time, int cvt = "+et+" time per
iteration = "+itI);
        System.out.println("Difference is " + (itD - itI));
    }

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to