[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Static Method performance



I did a small test-
The following code calls a static and a non-static method which effectively
do the same thing - some time consuming processing.
Run the code and you would see the static calls takes almost the double time
than the non-static call.

Any thoughts??

So if we maintain a pool of objects and let the calling party get an
instance from the pool and make a call--i guess that would be faster.

Thanks
Mayank
--------------------------------------------------------
public class Test1
{
  public static void main(String[] args)
  {
   long l1 = System.currentTimeMillis();

   Test2 t = new Test2();
   t.Met2();

   long l2 = System.currentTimeMillis();

   System.out.print("Non Static call - ");
   System.out.println(l2-l1);

   l1 = System.currentTimeMillis();

   Test2.Met1();
   l2 = System.currentTimeMillis();

   System.out.print("Static call - ");
   System.out.println(l2-l1);
  }

}

class Test2
{
 public static void Met1()
 {
  for(int i=0;i<10000;i++)
  {
   for(int j=0;j<10000;j++);
  }
 }

 public void Met2()
 {

  for(int i=0;i<10000;i++)
  {
   for(int j=0;j<10000;j++);
  }
 }

}
--------------------------------------------------------------
----- Original Message -----
From: "Curt Smith" <chsmith@speakeasy.net>
To: <ajug-members@www.ajug.org>
Cc: <ajug-members@www.ajug.org>
Sent: Wednesday, February 27, 2002 2:58 PM
Subject: Re: Static Method performance


>
> > Say there are 500 objects which have to use a certain method X (
exampled
> > below).
> > If i make X as static then 499 objects would have to queue up for access
to
> > the method,
>
>
> Note that static does not mean synchronized.
>
> Your example would allow all threads to access your data[] array
> concurrently.  This is ok, even safe if your data does not change
> at run time.  Even if it does change, there's several operations
> that the VM assures to be atomic.  Not enough space here for
> that.
>
> If data[] is big or expensive to create, this is a perfect candidate
> for a static method and members.   You can still have a synchronized
> mutator to change out the whole data array or just an index, leveraging
> atomicity of a variable equate operation.  But your business data
> may have more state that all needs to be consistant for this simplistic
> discussion about what's atomic and safe in a concurrent environment.
>
> re: the performance advantage, my guess is that the 15% perf advantage
> is entirely due to new object () and garbage collection for the
> non-static test case.  If the same object's method was being called
> and vs creating a new object each time,
> I'd be surprised that some vendor's JIT wouldn't be able to remove any
perf.
> difference.   TBD
>
> curt
>
>
> while if i make X an instance variable then each of 500 objects
> > would have their own copy of the X's object and hence a copy of X.
> >
> > public class Y
> > {
> > String[] data = new String[100];
> >
> > public String[] X(int i)
> > {
> >       return (data[i],data[i+1].....data[i+10]);
> > }
> >
> > }
> >
> > I understand that if i make X as static then data would also be a static
> > variable.
> >
> > Hope i have been able to express my question correctly.
> >
> > Thanks
> > Mayank
> >
> > ----- Original Message -----
> > From: "Brian Lee" <brian_a_lee@hotmail.com>
> > To: <aggarwalmayank@hotmail.com>; <ajug-members@www.ajug.org>
> > Sent: Wednesday, February 27, 2002 1:34 PM
> > Subject: Re: Static Method performance
> >
> >
> >
> >>I've seen about a 15% increase in performance between the same method
> >>
> > static and non-static. The test is pretty basic (do this 1000000 times
and
> > time it) so I don't have real-world data.
> >
> >>The only downside I've experienced with static methods is that if your
> >>
> > class is implementing an interface, the interface defined methods can't
be
> > static.
> >
> >>Other than this, I make as many methods as possible static (and final)
for
> >>
> > performance reasons. Since most of the code I write doesn't go into a
> > published API, I declare final and then change it if extending becomes
> > necessary.
> >
> >>BAL
> >>
> >>
> >>>From: "Mayank Aggarwal" <aggarwalmayank@hotmail.com>
> >>>To: <ajug-members@www.ajug.org>
> >>>Subject: Static Method performance
> >>>Date: Wed, 27 Feb 2002 13:02:57 -0500
> >>>MIME-Version: 1.0
> >>>X-Originating-IP: [156.63.134.4]
> >>>Received: from [66.45.18.180] by hotmail.com (3.2) with ESMTP id
> >>>
> > MHotMailBE466B1300274004318B422D12B487C50; Wed, 27 Feb 2002
10:06:24 -0800
> >
> >>>Received: (from list@localhost)by ajug.org (8.11.2/8.11.2) id
> >>>
> > g1RJAS514324;Wed, 27 Feb 2002 14:10:28 -0500
> >
> >>>From ajug-members-request@ajug.org Wed, 27 Feb 2002 10:07:33 -0800
> >>
> >>>Resent-Date: Wed, 27 Feb 2002 14:10:28 -0500
> >>>X-Priority: 3
> >>>X-MSMail-Priority: Normal
> >>>X-Mailer: Microsoft Outlook Express 5.50.4807.1700
> >>>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4807.1700
> >>>Message-ID: <OE48P1f3L0zGSOpD0mA0000c25f@hotmail.com>
> >>>X-OriginalArrivalTime: 27 Feb 2002 18:03:41.0818 (UTC)
> >>>
> > FILETIME=[16F64DA0:01C1BFB9]
> >
> >>>Resent-Message-ID: <Fvd7UB.A.ufD.k8Sf8@ajug.org>
> >>>Resent-From: ajug-members@ajug.org
> >>>X-Mailing-List: <ajug-members@ajug.org> archive/latest/1
> >>>X-Loop: ajug-members@ajug.org
> >>>List-Post: <mailto:ajug-members@ajug.org>
> >>>List-Help: <mailto:ajug-members-request@ajug.org?subject=help>
> >>>List-Subscribe:
<mailto:ajug-members-request@ajug.org?subject=subscribe>
> >>>List-Unsubscribe:
> >>>
> > <mailto:ajug-members-request@ajug.org?subject=unsubscribe>
> >
> >>>Precedence: list
> >>>Resent-Sender: ajug-members-request@ajug.org
> >>>
> >>>How does static method affect performance in Java?
> >>>
> >>>I understand that all the method variables are created on Stack and
hence
> >>>
> > each calling party gets its own stack of variables, hence there should
not
> > be problems of concurrency as long as only method local variables are
> > manipulated.
> >
> >>>What other reasons are there for not using a static methods ?
> >>>
> >>>
> >>>Thanks
> >>>Mayank
> >>>
> >>
> >>
> >>
> >
>
>
> --
>
> Curt Smith
> chsmith@speakeasy.net
> (w) 404-463-0973
> (h) 404-294-6686
>
>
>
>