Naive number questions

Rob Lally smalltalk at roblally.plus.com
Wed Nov 3 16:45:10 UTC 2004


Here are the timings on my machine using JDK 1.5 and the autoboxing 
facilities

Squeak 3.7                                         -> 23899
JDK 1.5, java primitives                      -> 2844
JDK 1.5, autoboxing and wrappers     -> 60487

So idiomatic Java is an order of magnitude faster than Squeak in this 
test(if it is worth calling it a test), but squeak is twice or so as 
fast if you use primitive wrappers and autoboxing.

Of course this is a highly contrived example, purpose designed to make 
java look bad by using contrived non-idiomatic code.


Rob

.............................................................................................
class NumtestIdiomatic
{
    public static void main(String args[])
    {
        long x = 0;

        long timeStart = System.currentTimeMillis();

        for (long i = 0; i < 2; i++)
            for (long j = 0; j < 100000000; j++)
                x++;

        System.out.println(System.currentTimeMillis() - timeStart);
        System.out.println(x);
    }
}
.............................................................................................
class NumtestWrappersAndAutoboxing
{
    public static void main(String args[])
    {
        Long x = 0l;

        Long timeStart = System.currentTimeMillis();

        for (Long i = 0l; i < 2; i++)
            for (Long j = 0l; j < 100000000; j++)
                x++;

        System.out.println(System.currentTimeMillis() - timeStart);
        System.out.println(x);
    }
}
.............................................................................................


Bert Freudenberg wrote:

> Am 02.11.2004 um 22:42 schrieb Andreas Raab:
>
>> Besides, one of the more interesting things to do is to not use 
>> "long/int" but rather the Object integer type.
>
>
> Yep. On my machine the speed then is almost equal. Mind you, Squeak 
> Interpreter vs. Java JIT:
>
>     | x |
>     x := 0.
>     (Time millisecondsToRun: [
>         1 to: 2 do: [: i|
>             1 to: 100000000 do: [:j | x := x + 1]]]) -> x
>
> 80244->200000000
>
> =================
>
> class Numtest
> {
>     public static void main(String args[]) {
>     Long x;
>     Long timeStart;
>        Integer one = new Integer(1);
>     
>     x = new Long(0);
>     timeStart = new Long(System.currentTimeMillis());
>     
>     for (Integer i = new Integer(0); i.intValue() < 2; i=new 
> Integer(i.intValue()+one.intValue())) {
>         for (Integer j = new Integer(0); j.intValue() < 100000000; 
> j=new Integer(j.intValue()+one.intValue())) {
>         x = new Long(x.longValue() + one.longValue());
>         }
>     }
>     
>     timeStart = new Long(System.currentTimeMillis() - 
> timeStart.longValue());
>     System.out.println(timeStart.toString());
>     System.out.println(x.toString());
>     }
> }
>
> % java -version
> java version "1.4.2_05"
> Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-141.3)
> Java HotSpot(TM) Client VM (build 1.4.2-38, mixed mode)
> % javac Numtest.java && java Numtest
> 66851
> 200000000
>
> ==============
>
> Squeak: 80 seconds
> Java: 66 seconds
>
> Meaning they are almost equal, with Java being faster by 20%, but by 
> not several orders of magnitude. Not bad for a pure Interpreter, I'd 
> say. Would be interesting to try with Auto-Boxing/Unboxing, but I 
> don't have a newer Java version installed.
>
> - Bert -
>
>
>




More information about the Squeak-dev mailing list