28. april 2004 - 12:09Der er
12 kommentarer og 1 løsning
Performance between public, protected, package and private
Hello!
I thought that I would see a performance improvement by making a method private instead of public. So I wrote this test program to prove my point. But I didn't see any improvment at all????.
So now is the question is there any performance improvement by making a method private instead of public, or have I dreamt it?
Best regards Fredrik
Below is a testprogram that runs the same code within different methods (public, protected, package and private).
public class PerformanceTest { long time; public PerformanceTest() { //Just a little warm up String str = new String(""); for(int i = 0; i < 1000; i++) str = str + i;
time = System.currentTimeMillis(); publicMethod(); time = System.currentTimeMillis() -time; System.out.println("public: " + time);
time = System.currentTimeMillis(); protectedMethod(); time = System.currentTimeMillis() -time; System.out.println("protected: " + time);
time = System.currentTimeMillis(); packageMethod(); time = System.currentTimeMillis() -time; System.out.println("package: " + time);
time = System.currentTimeMillis(); privateMethod(); time = System.currentTimeMillis() -time; System.out.println("private: " + time); }
public final void publicMethod() { String str = new String(""); for(int i = 0; i < 3000; i++) str = str + i; }
public void protectedMethod() { String str = new String(""); for(int i = 0; i < 3000; i++) str = str + i; }
void packageMethod() { String str = new String(""); for(int i = 0; i < 3000; i++) str = str + i; }
private void privateMethod() { String str = new String(""); for(int i = 0; i < 3000; i++) str = str + i; }
public static void main(String[] args) { new PerformanceTest(); } }
Visibility determines at compile what you are allowed to call.
Performance is runtime characteristica.
The only argument should be that the oprmizer could more freely optimize something private because it knows what will use it. But that does not sound like how Java does it.
There are no real 'rules' about optimizing except the big one "the program MUST work as specified by the language rules". So each vendor generating a Java compiler claim to have done it better in whichever way. Even Suns Javacompiler today optimize more (and differently) than it did 2 years ago.
"While information hiding does not, in and of itself, cause good performance, it eanables effective performance tuning."
[og det udpensles så at når implementationen er skjult så kan man efter at have verificeret applikationens korrekthed identificere flaskehalsene og optimere implementationen uden at frygte at breake noget]
Jeg koder en del i J2ME, og der er der performance forskel mellem static og non-static. Har dog ingen præcise tal. Jeg går ud fra at det også gælder i J2SE/J2EE. Men det kan også skyldes frigørelse af den begrænsede hukommelse i J2ME miljøet.
Det gælder allevegne. static variable kan udlægges med en absolut memory adresse; mens non-static variable allokeres ved runtime og addresseres med et offset til objektreferencen.
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.