Newsgroup: comp.lang.java.programmer


Date: Mon, 24 Apr 2006 00:26:14 +0300
From: Diomidis Spinellis <dds@aueb.gr>
Organization: Athens University of Economics and Business
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2) Gecko/20060404 SeaMonkey/1.0.1
MIME-Version: 1.0
Newsgroups: comp.lang.java.programmer
Subject: Re: strange performance behavior of a mathematical method: why?
References: <1145823428.424885.264990@u72g2000cwu.googlegroups.com>
In-Reply-To: <1145823428.424885.264990@u72g2000cwu.googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Dimitri Ognibene wrote:
> I've found a difference of about 10% in the execution of 2 version of
> this method:
> 
> public double[] evaluate(double input[]){
>         double a;
>         //System.arraycopy(input,0,activation[0],0,input.length);
>         activation[0]=input;
>         //for (int i=0;i<input.length;i++)
>         //  activation[0][i]=input[i];
>         for (int i=1;i<layers;i++){
>             double activation_col[]=activation[i-1];
>             double activation_col_res[]=activation[i];
>             double weight_matr[][]=weight[i-1];
>             for (int j =0; j< activation_col_res.length;j++ ){
>                 double weight_col[]=weight_matr[j];
>                 double acc=0;
>                 for (int k=0; k<activation_col.length;k++){
>                    *************variant go here*************
>                 }
>                 activation_col_res[j]=g(acc);
>             }
>         }
>         setChanged();
>         notifyObservers();
> 
>         return activation[layers-1];
> 
> 
>     }
> 
> variant 1:
>                      a= activation_col[k];
>                     a*=weight_col[k];
>                     acc+=a;
> variant 2:
>                     acc+= activation_col[k]*weight_col[k];
> 
> variant1 is 10% faster than variant2.
> I've a matrix of about 1200x400 elements (weight matrix)
> variant1 avarage is 4.82ms
> variant2 avarage is 5.24ms
> 
> does this performance difference makes any sense?

If you are curious look at the generated Java bytecodes (use javap). 
But the difference isn't important.  Another compiler, jit or processor 
architecture could give you different results.

> does someone has any tips to write always the faster code?

- Measure before optimizing
- Focus on algorithmic improvements

-- 
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006)
http://www.spinellis.gr/codequality?cljp



Newsgroup comp.lang.java.programmer contents
Newsgroup list
Diomidis Spinellis home page

Creative Commons License Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-Share Alike 3.0 Greece License.