Created
November 12, 2012 19:41
-
-
Save tecnocrata/4061445 to your computer and use it in GitHub Desktop.
Testing Static Methods with Multi-Thread behaviour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class StaticSpeedDecrease | |
| { | |
| static final int ITERATIONS = 100; | |
| static final long MAX_VALUE = 100000; | |
| public static void main(final String[] args) | |
| { | |
| int maxThreads = 100; | |
| boolean isStatic = args.length > 0; | |
| Thread[] threads = new Thread[maxThreads]; | |
| Runnable task = (isStatic) ? Worker.staticRunner : Worker.instanceRunner; | |
| for (int i = 0; i < maxThreads; i++) | |
| { | |
| threads[i] = new Thread(task); | |
| } | |
| long startTime = System.currentTimeMillis(); | |
| for (int i = 0; i < maxThreads; i++) | |
| { | |
| threads[i].start(); | |
| } | |
| for (int i = 0; i < maxThreads; i++) | |
| { | |
| try | |
| { | |
| threads[i].join(); | |
| } | |
| catch (InterruptedException e) | |
| { | |
| // Oh well; | |
| } | |
| } | |
| long time = System.currentTimeMillis() - startTime; | |
| System.out.printf("It took %dms to run %d Threads with %s method calls.%n", time, maxThreads, | |
| ((isStatic) ? "static" : "non-static")); | |
| } | |
| } | |
| class Worker | |
| { | |
| public static final Runnable staticRunner = new Runnable() | |
| { | |
| public void run() | |
| { | |
| staticFakeWork(); | |
| } | |
| }; | |
| public static final Runnable instanceRunner = new Runnable() | |
| { | |
| Worker w = new Worker(); | |
| public void run() | |
| { | |
| this.w.instanceFakeWork(); | |
| } | |
| }; | |
| public static void staticFakeWork() | |
| { | |
| long p = 0; | |
| for (int i = 0; i < StaticSpeedDecrease.ITERATIONS; i++) | |
| { | |
| for (long l = 0; l < StaticSpeedDecrease.MAX_VALUE; l++) | |
| { | |
| p += l % 400; | |
| } | |
| } | |
| reportFakeData(p); | |
| } | |
| public void instanceFakeWork() | |
| { | |
| long p = 0; | |
| for (int i = 0; i < StaticSpeedDecrease.ITERATIONS; i++) | |
| { | |
| for (long l = 0; l < StaticSpeedDecrease.MAX_VALUE; l++) | |
| { | |
| p += l % 400; | |
| } | |
| } | |
| reportDataFake(p); | |
| } | |
| static void reportFakeData(final long v) | |
| { | |
| if (v < 5) | |
| { | |
| System.out.println("Wow"); | |
| } | |
| } | |
| void reportDataFake(final long v) | |
| { | |
| if (v < 5) | |
| { | |
| System.out.println("Wow"); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note, the code was designed to make it so the work consumes CPU and the loop can't be optimized away (the JVM seemed much better at optimizing the static content then the non-static content).
You run the code like:
java forums.tests.speed.StaticSpeedDecrease
to get non-static runs and
java forums.tests.speed.StaticSpeedDecrease static
to get static runs.
On my machine, my (4) CPUs spiked during the majority of the run and the results were very consistent. Over about 30 or so runs each, static code ran 30 ms slower than non-static code (on average). If you took any 4 trials and grouped them together then averaged those runs you would see the same 30 ms difference between static and non-static runs.
My guess would have been there was no difference so I am thoroughly surprised to see that there is an measurable (if small) difference between them. Still, I think this comes under the micro-optimization category. , I would let the class design and usage dictate my choice between static / non-static rather than possible speed gains. I don't understand where that speed gain is coming from but it is minor and it is may be runtime specific.
http://www.coderanch.com/t/231982/threads/java/Static-Methods-local-variables-Thread