Advertisement
Guest User

Pre/Post Incrementation Test

a guest
Jul 24th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.11 KB | None | 0 0
  1. //Original code made by Reese Moore for http://stackoverflow.com/questions/4110764/very-basic-code-performance
  2.  
  3. //This version made by Amndeep Singh Mann
  4.  
  5. //Modified to further test pre/post incrementation.
  6.  
  7. public class TestIncrement
  8. {
  9.     public static int MooreMethod()
  10.     {
  11.         //System.out.println("MooreMethod");
  12.  
  13.         int x = 0;
  14.         int y = 0;
  15.         long startTime;
  16.  
  17.         //System.out.println("Start postcrement");
  18.         // Postincrement
  19.         startTime = System.currentTimeMillis();
  20.         while ( startTime + 1000 > System.currentTimeMillis())
  21.         {
  22.             x++;
  23.         }
  24.  
  25.         //System.out.println("Start precement");
  26.         // Preincrement
  27.         startTime = System.currentTimeMillis();
  28.         while ( startTime + 1000 > System.currentTimeMillis())
  29.         {
  30.             ++y;
  31.         }
  32.  
  33.         // Output
  34.         //System.out.println("Postincrement: " + x);
  35.         //System.out.println("Preincrement:  " + y);
  36.  
  37.         return x - y;
  38.     }
  39.  
  40.     public static int MooreExtremeMethod()
  41.     {
  42.         //System.out.println("MooreExtremeMethod");
  43.  
  44.         int x = 0;
  45.         int y = 0;
  46.         long startTime;
  47.  
  48.         // Postincrement
  49.         //System.out.println("Start postcrement");
  50.         startTime = System.currentTimeMillis();
  51.         while ( startTime + 10000 > System.currentTimeMillis())
  52.         {
  53.             x++;
  54.         }
  55.  
  56.         // Preincrement
  57.         //System.out.println("Start precement");
  58.         startTime = System.currentTimeMillis();
  59.         while ( startTime + 10000 > System.currentTimeMillis())
  60.         {
  61.             ++y;
  62.         }
  63.  
  64.         // Output
  65.         //System.out.println("Postincrement: " + x);
  66.         //System.out.println("Preincrement:  " + y);
  67.  
  68.         return x - y;
  69.     }
  70.  
  71.     public static int MannMethod()
  72.     {
  73.         //System.out.println("MannMethod");
  74.  
  75.         int x = 0;
  76.         int y = 0;
  77.         long startTime;
  78.  
  79.         // Postincrement
  80.         //System.out.println("Start postcrement");
  81.         startTime = System.currentTimeMillis();
  82.         for(; startTime + 1000 > System.currentTimeMillis(); x++)
  83.         {
  84.             Math.sqrt(x);
  85.         }
  86.  
  87.         // Preincrement
  88.         //System.out.println("Start precement");
  89.         startTime = System.currentTimeMillis();
  90.         for(; startTime + 1000 > System.currentTimeMillis(); ++y)
  91.         {
  92.             Math.sqrt(y);
  93.         }
  94.  
  95.         // Output
  96.         //System.out.println("Postincrement: " + x);
  97.         //System.out.println("Preincrement:  " + y);
  98.  
  99.         return x - y;
  100.     }
  101.  
  102.     public static int MannlierMethod()
  103.     {
  104.         //System.out.println("MannlierMethod");
  105.  
  106.         int x = 0;
  107.         int y = 0;
  108.         long startTime;
  109.  
  110.         // Postincrement
  111.         //System.out.println("Start postcrement");
  112.         startTime = System.currentTimeMillis();
  113.         for(; startTime + 10000 > System.currentTimeMillis(); x++)
  114.         {
  115.             Math.sqrt(x);
  116.         }
  117.  
  118.         // Preincrement
  119.         //System.out.println("Start precement");
  120.         startTime = System.currentTimeMillis();
  121.         for(; startTime + 10000 > System.currentTimeMillis(); ++y)
  122.         {
  123.             Math.sqrt(y);
  124.         }
  125.  
  126.         // Output
  127.         //System.out.println("Postincrement: " + x);
  128.         //System.out.println("Preincrement:  " + y);
  129.  
  130.         return x - y;
  131.     }
  132.  
  133.     public static int ReverseMooreMethod()
  134.     {
  135.         //System.out.println("ReverseMooreMethod");
  136.  
  137.         int x = 0;
  138.         int y = 0;
  139.         long startTime;
  140.  
  141.         //System.out.println("Start precement");
  142.         // Preincrement
  143.         startTime = System.currentTimeMillis();
  144.         while ( startTime + 1000 > System.currentTimeMillis())
  145.         {
  146.             ++y;
  147.         }
  148.  
  149.         //System.out.println("Start postcrement");
  150.         // Postincrement
  151.         startTime = System.currentTimeMillis();
  152.         while ( startTime + 1000 > System.currentTimeMillis())
  153.         {
  154.             x++;
  155.         }
  156.  
  157.         // Output
  158.         //System.out.println("Postincrement: " + x);
  159.         //System.out.println("Preincrement:  " + y);
  160.  
  161.         return x - y;
  162.     }
  163.  
  164.     public static int ReverseMooreExtremeMethod()
  165.     {
  166.         //System.out.println("ReverseMooreExtremeMethod");
  167.  
  168.         int x = 0;
  169.         int y = 0;
  170.         long startTime;
  171.  
  172.         // Preincrement
  173.         //System.out.println("Start precement");
  174.         startTime = System.currentTimeMillis();
  175.         while ( startTime + 10000 > System.currentTimeMillis())
  176.         {
  177.             ++y;
  178.         }
  179.  
  180.         // Postincrement
  181.         //System.out.println("Start postcrement");
  182.         startTime = System.currentTimeMillis();
  183.         while ( startTime + 10000 > System.currentTimeMillis())
  184.         {
  185.             x++;
  186.         }
  187.  
  188.         // Output
  189.         //System.out.println("Postincrement: " + x);
  190.         //System.out.println("Preincrement:  " + y);
  191.  
  192.         return x - y;
  193.     }
  194.  
  195.     public static int ReverseMannMethod()
  196.     {
  197.         //System.out.println("ReverseMannMethod");
  198.  
  199.         int x = 0;
  200.         int y = 0;
  201.         long startTime;
  202.  
  203.         // Preincrement
  204.         //System.out.println("Start precement");
  205.         startTime = System.currentTimeMillis();
  206.         for(; startTime + 1000 > System.currentTimeMillis(); ++y)
  207.         {
  208.             Math.sqrt(y);
  209.         }
  210.  
  211.         // Postincrement
  212.         //System.out.println("Start postcrement");
  213.         startTime = System.currentTimeMillis();
  214.         for(; startTime + 1000 > System.currentTimeMillis(); x++)
  215.         {
  216.             Math.sqrt(x);
  217.         }
  218.  
  219.         // Output
  220.         //System.out.println("Postincrement: " + x);
  221.         //System.out.println("Preincrement:  " + y);
  222.  
  223.         return x - y;
  224.     }
  225.  
  226.     public static int ReverseMannlierMethod()
  227.     {
  228.         //System.out.println("ReverseMannlierMethod");
  229.  
  230.         int x = 0;
  231.         int y = 0;
  232.         long startTime;
  233.  
  234.         // Preincrement
  235.         //System.out.println("Start precement");
  236.         startTime = System.currentTimeMillis();
  237.         for(; startTime + 10000 > System.currentTimeMillis(); ++y)
  238.         {
  239.             Math.sqrt(y);
  240.         }
  241.  
  242.         // Postincrement
  243.         //System.out.println("Start postcrement");
  244.         startTime = System.currentTimeMillis();
  245.         for(; startTime + 10000 > System.currentTimeMillis(); x++)
  246.         {
  247.             Math.sqrt(x);
  248.         }
  249.  
  250.         // Output
  251.         //System.out.println("Postincrement: " + x);
  252.         //System.out.println("Preincrement:  " + y);
  253.  
  254.         return x - y;
  255.     }
  256.  
  257.  
  258.     public static void main(String[] av)
  259.     {
  260.         System.out.println("Negative values means that the preincrement operator had more usages.");
  261.        
  262.         int loop, comparison, totalComparison;
  263.  
  264.         for(totalComparison = loop = comparison = 0; loop < 5; loop++, comparison = 0)
  265.         {
  266.             comparison += MooreMethod();
  267.             comparison += MooreExtremeMethod();
  268.             comparison += MannMethod();
  269.             comparison += MannlierMethod();
  270.             comparison += ReverseMooreMethod();
  271.             comparison += ReverseMooreExtremeMethod();
  272.             comparison += ReverseMannMethod();
  273.             comparison += ReverseMannlierMethod();
  274.  
  275.             totalComparison += comparison;
  276.  
  277.             System.out.println(comparison);
  278.         }
  279.  
  280.         System.out.println("Total: " + totalComparison);
  281.     }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement