Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Original code made by Reese Moore for http://stackoverflow.com/questions/4110764/very-basic-code-performance
- //This version made by Amndeep Singh Mann
- //Modified to further test pre/post incrementation.
- public class TestIncrement
- {
- public static int MooreMethod()
- {
- //System.out.println("MooreMethod");
- int x = 0;
- int y = 0;
- long startTime;
- //System.out.println("Start postcrement");
- // Postincrement
- startTime = System.currentTimeMillis();
- while ( startTime + 1000 > System.currentTimeMillis())
- {
- x++;
- }
- //System.out.println("Start precement");
- // Preincrement
- startTime = System.currentTimeMillis();
- while ( startTime + 1000 > System.currentTimeMillis())
- {
- ++y;
- }
- // Output
- //System.out.println("Postincrement: " + x);
- //System.out.println("Preincrement: " + y);
- return x - y;
- }
- public static int MooreExtremeMethod()
- {
- //System.out.println("MooreExtremeMethod");
- int x = 0;
- int y = 0;
- long startTime;
- // Postincrement
- //System.out.println("Start postcrement");
- startTime = System.currentTimeMillis();
- while ( startTime + 10000 > System.currentTimeMillis())
- {
- x++;
- }
- // Preincrement
- //System.out.println("Start precement");
- startTime = System.currentTimeMillis();
- while ( startTime + 10000 > System.currentTimeMillis())
- {
- ++y;
- }
- // Output
- //System.out.println("Postincrement: " + x);
- //System.out.println("Preincrement: " + y);
- return x - y;
- }
- public static int MannMethod()
- {
- //System.out.println("MannMethod");
- int x = 0;
- int y = 0;
- long startTime;
- // Postincrement
- //System.out.println("Start postcrement");
- startTime = System.currentTimeMillis();
- for(; startTime + 1000 > System.currentTimeMillis(); x++)
- {
- Math.sqrt(x);
- }
- // Preincrement
- //System.out.println("Start precement");
- startTime = System.currentTimeMillis();
- for(; startTime + 1000 > System.currentTimeMillis(); ++y)
- {
- Math.sqrt(y);
- }
- // Output
- //System.out.println("Postincrement: " + x);
- //System.out.println("Preincrement: " + y);
- return x - y;
- }
- public static int MannlierMethod()
- {
- //System.out.println("MannlierMethod");
- int x = 0;
- int y = 0;
- long startTime;
- // Postincrement
- //System.out.println("Start postcrement");
- startTime = System.currentTimeMillis();
- for(; startTime + 10000 > System.currentTimeMillis(); x++)
- {
- Math.sqrt(x);
- }
- // Preincrement
- //System.out.println("Start precement");
- startTime = System.currentTimeMillis();
- for(; startTime + 10000 > System.currentTimeMillis(); ++y)
- {
- Math.sqrt(y);
- }
- // Output
- //System.out.println("Postincrement: " + x);
- //System.out.println("Preincrement: " + y);
- return x - y;
- }
- public static int ReverseMooreMethod()
- {
- //System.out.println("ReverseMooreMethod");
- int x = 0;
- int y = 0;
- long startTime;
- //System.out.println("Start precement");
- // Preincrement
- startTime = System.currentTimeMillis();
- while ( startTime + 1000 > System.currentTimeMillis())
- {
- ++y;
- }
- //System.out.println("Start postcrement");
- // Postincrement
- startTime = System.currentTimeMillis();
- while ( startTime + 1000 > System.currentTimeMillis())
- {
- x++;
- }
- // Output
- //System.out.println("Postincrement: " + x);
- //System.out.println("Preincrement: " + y);
- return x - y;
- }
- public static int ReverseMooreExtremeMethod()
- {
- //System.out.println("ReverseMooreExtremeMethod");
- int x = 0;
- int y = 0;
- long startTime;
- // Preincrement
- //System.out.println("Start precement");
- startTime = System.currentTimeMillis();
- while ( startTime + 10000 > System.currentTimeMillis())
- {
- ++y;
- }
- // Postincrement
- //System.out.println("Start postcrement");
- startTime = System.currentTimeMillis();
- while ( startTime + 10000 > System.currentTimeMillis())
- {
- x++;
- }
- // Output
- //System.out.println("Postincrement: " + x);
- //System.out.println("Preincrement: " + y);
- return x - y;
- }
- public static int ReverseMannMethod()
- {
- //System.out.println("ReverseMannMethod");
- int x = 0;
- int y = 0;
- long startTime;
- // Preincrement
- //System.out.println("Start precement");
- startTime = System.currentTimeMillis();
- for(; startTime + 1000 > System.currentTimeMillis(); ++y)
- {
- Math.sqrt(y);
- }
- // Postincrement
- //System.out.println("Start postcrement");
- startTime = System.currentTimeMillis();
- for(; startTime + 1000 > System.currentTimeMillis(); x++)
- {
- Math.sqrt(x);
- }
- // Output
- //System.out.println("Postincrement: " + x);
- //System.out.println("Preincrement: " + y);
- return x - y;
- }
- public static int ReverseMannlierMethod()
- {
- //System.out.println("ReverseMannlierMethod");
- int x = 0;
- int y = 0;
- long startTime;
- // Preincrement
- //System.out.println("Start precement");
- startTime = System.currentTimeMillis();
- for(; startTime + 10000 > System.currentTimeMillis(); ++y)
- {
- Math.sqrt(y);
- }
- // Postincrement
- //System.out.println("Start postcrement");
- startTime = System.currentTimeMillis();
- for(; startTime + 10000 > System.currentTimeMillis(); x++)
- {
- Math.sqrt(x);
- }
- // Output
- //System.out.println("Postincrement: " + x);
- //System.out.println("Preincrement: " + y);
- return x - y;
- }
- public static void main(String[] av)
- {
- System.out.println("Negative values means that the preincrement operator had more usages.");
- int loop, comparison, totalComparison;
- for(totalComparison = loop = comparison = 0; loop < 5; loop++, comparison = 0)
- {
- comparison += MooreMethod();
- comparison += MooreExtremeMethod();
- comparison += MannMethod();
- comparison += MannlierMethod();
- comparison += ReverseMooreMethod();
- comparison += ReverseMooreExtremeMethod();
- comparison += ReverseMannMethod();
- comparison += ReverseMannlierMethod();
- totalComparison += comparison;
- System.out.println(comparison);
- }
- System.out.println("Total: " + totalComparison);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement