Anto Telvin Mathew<br />Many of the life failures are people who did not realize how close they were to success when they give up. EDISON
Anto Telvin Mathew<br />Many of the life failures are people who did not realize how close they were to success when they give up. EDISON
Anto Telvin Mathew<br />Many of the life failures are people who did not realize how close they were to success when they give up. EDISON
Originally posted by Campbell Ritchie:
. . . And you do realise the first two loops are not equivalent to each other, nor reversed forms of each other?
[ October 11, 2008: Message edited by: Campbell Ritchie ]
"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Originally posted by Campbell Ritchie:
One of them might do 100000 times. Count the two very carefully and see how many times they run.
"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
So the second loop will doubtless be much quicker, only having to iterate 99999 times.
What I notice is that there is considerable variation in the times, and (in some instances) the slower times for algorithm A are slower than the faster times for algorithm B, and there is one instance where algorithm B is actually faster. So I have given up worrying about that sort of performance question.Algorithm A Algorithm B
12.56 16.87
11.76 17.80
14.97 16.50
18.32 18.31
11.97 12.87
16.82 18.97
What irony?
I randomly set it to go up first or down first to avoid optimisation effects, and here I have two downs faster and one up faster, but the actual times vary between 2.3ms and 36ms. The "idle" time is how long it takes to call the System#nanoTime() method twice and subtract the results. Just goes to show how unpredictable performance can be.java trivia.LoopSpeedTest 1000000
Time when idle 2.949�s
Time when ascending 2919.964�s
Time when descending 2302.834�s
Time when idle 2.175�s
[Campbell@queeg applications]$ java trivia.LoopSpeedTest 1000000
Time when idle 2.290�s
Time when descending 36417.470�s
Time when ascending 5343.394�s
Time when idle 1.697�s
[Campbell@queeg applications]$ java trivia.LoopSpeedTest 1000000
Time when idle 2.579�s
Time when ascending 30165.420�s
Time when descending 6139.734�s
Time when idle 1.965�s
[Campbell@queeg applications]$
Originally posted by myself:
What irony?
Answer to B: Both should not have much difference if they are properly benchmarked and given enough time.
Reason: Here is the Math.max(x,y) code
public static int max(int a, int b) {
return (a >= b) ? a : b;
}
And runtime automatically inlines these sort of things to remove deadcode. So ideally you shouldn't find any major boost with A/B
SCJP, SCWCD.
|Asking Good Questions|
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Originally posted by R K Singh:
Most of the time I saw that performance is not poor because of these small programming techniques but the real problems were because of poor logic, unnecessary repeated calls to DB, poor joins in SQL, poor configuration of app server etc.
my 2 cents.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
my 2 cents.
http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Author of VFSJFileChooser and XPontus XML Editor
Originally posted by Yves Zoundi:
1) For the first code, 1) comparison to zero in a loop is usually faster
2) For the second code, I suspect that 2) will be faster as it doesn't involve any method call.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Consider Paul's rocket mass heater. |