[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Static Method performance
When I ran my tests and saw 15% increase I wasn't creating a new object for
every call.
It was something like this:
for (i < 1 billion)
MyClass.staticMethod
vs.
MyObject o
for (i < 1 billion)
o.nonStaticMethod
Also, a note on "performance testing" via loops of fury:
I usually have two values, the first is the number of tests, the second is
the number of loop iterations.
Each test is n loop iterations. Then I average the total number of tests.
You'll see times fluctuate pretty broadly so you need a large sample of
tests to average.
something like this test class (pretty long, ignore if not interested)
/*
2001.4.10 bal
This class tests the time taken to compare a string to a zero length string
("") using .equals()
and .length().
The results show that .length() is 75% faster than .equals
*/
class testCheckEquals{
public static void main (String [] args){
byte bb = 6;
int ii = bb;
long l1,l2;
l1 = System.currentTimeMillis();
int its = 0;
int loops = 0;
String testval = null;
try{
loops = Integer.parseInt(args[0]);
its = Integer.parseInt(args[1]);
testval = args[2];
} catch (Exception e){
System.out.println("Usage: java testCheckEquals <number of test loops>
<iteration per loop> <string to be tested>");
System.exit(0);
}
float [] lengths = new float[loops];
float [] equals = new float[loops];
for (int i=0; i<loops;i++){
lengths[i] = runLengthTest(testval, its);
equals[i] = runEqualsTest(testval, its);
}
// calc the avgs and stuff
float avglens = 0.0f;
float avgeqls = 0.0f;
float sumlens = 0.0f;
float sumeqls = 0.0f;
for (int i=0; i<loops;i++){
System.out.println("Length " + i + ": " + lengths[i]);
System.out.println("Equals " + i + ": " + equals[i]);
sumlens+=lengths[i];
sumeqls+=equals[i];
}
avglens = sumlens / loops;
avgeqls = sumeqls / loops;
float dif = Math.abs(avglens - avgeqls);
System.out.println("Average seconds to run Length check: " + avglens);
System.out.println("Average seconds to run Equals check: " + avgeqls);
System.out.println("Difference in seconds: " + dif);
System.out.println("Difference %: " +
(dif/((float)Math.max(avglens,avgeqls))*100f));
l2 = System.currentTimeMillis();
System.out.println("End time: " + l1);
System.out.println("Time elapsed: " + ((float)(l2 - l1) / 1000f));
boolean b;
l1 = System.currentTimeMillis();
for (int i = 0; i< its; i++){
b = (testval.length()==0);
}
l2 = System.currentTimeMillis();
System.out.println("End time: " + l1);
System.out.println("Time elapsed: " + ((float)(l2 - l1) / 1000f));
}
public static float runLengthTest(String testval, int iterations){
boolean b = false;
long l1,l2;
l1 = System.currentTimeMillis();
for (int i = 0; i< iterations; i++){
b = (testval.trim().length() == 0);
}
l2 = System.currentTimeMillis();
return ((float)(l2 - l1) / 1000f);
}
public static float runEqualsTest(String testval, int iterations){
boolean b = false;
long l1,l2;
l1 = System.currentTimeMillis();
for (int i = 0; i< iterations; i++){
b = (testval.trim().equals(""));
}
l2 = System.currentTimeMillis();
return ((float)(l2 - l1) / 1000f);
}
}
>From: Curt Smith <chsmith@speakeasy.net>
>To: <ajug-members@www.ajug.org>
>CC: ajug-members@www.ajug.org
>Subject: Re: Static Method performance
>Date: Wed, 27 Feb 2002 14:58:27 -0500
>MIME-Version: 1.0
>Received: from [66.45.18.180] by hotmail.com (3.2) with ESMTP id
>MHotMailBE46844200BA4136E81D422D12B48C4E0; Wed, 27 Feb 2002 11:53:45 -0800
>Received: (from list@localhost)by ajug.org (8.11.2/8.11.2) id
>g1RKx2816472;Wed, 27 Feb 2002 15:59:02 -0500
>From ajug-members-request@ajug.org Wed, 27 Feb 2002 11:54:45 -0800
>Resent-Date: Wed, 27 Feb 2002 15:59:02 -0500
>Message-Id: <02Feb27.150245est.134941@gdc2.dcor.state.ga.us>
>User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:0.9.4)
>Gecko/20011128 Netscape6/6.2.1
>X-Accept-Language: en-us
>References: <OE20qLBu3QMpvWYAFKk00007dc9@hotmail.com>
>Resent-Message-ID: <ByifJB.A.SBE.WiUf8@ajug.org>
>Resent-From: ajug-members@ajug.org
>X-Mailing-List: <ajug-members@ajug.org> archive/latest/5
>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
>
>
>>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
>
>
>
_________________________________________________________________
MSN Photos is the easiest way to share and print your photos:
http://photos.msn.com/support/worldwide.aspx