Advertisement
Guest User

Untitled

a guest
Feb 17th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.dumptruckman.test;
  2.  
  3. public class FloorTest {
  4.  
  5.     static int iterations = 5000;
  6.  
  7.     public static void main(String[] args) {
  8.         double x = 204.75764;
  9.         double z = -137.25428;
  10.         System.out.println(floor(x) + " == " + cast(x));
  11.         System.out.println(floor(z) + " == " + cast(z));
  12.         testFloor(x);
  13.         testFloor(z);
  14.         testCast(x);
  15.         testCast(z);
  16.     }
  17.    
  18.     public static void testFloor(double loc) {
  19.         long startTime = System.nanoTime();
  20.         for (int i = 0; i < iterations; i++) {
  21.             floor(loc);
  22.         }
  23.         long timeTaken = System.nanoTime() - startTime;
  24.         System.out.println("Time taken: " + timeTaken / 1000 + "ms");
  25.     }
  26.    
  27.     public static void testCast(double loc) {
  28.         long startTime = System.nanoTime();
  29.         for (int i = 0; i < iterations; i++) {
  30.             cast(loc);
  31.         }
  32.         long timeTaken = System.nanoTime() - startTime;
  33.         System.out.println("Time taken: " + timeTaken / 1000 + "ms");
  34.     }
  35.  
  36.     public static int floor(double loc) {
  37.         return (int) Math.floor(loc);
  38.     }
  39.  
  40.     public static int cast(double loc) {
  41.         int result = (int) loc;
  42.         return loc < 0 ? result - 1 : result;
  43.     }
  44. }
  45.  
  46. // OUTPUT:
  47. 204 == 204
  48. -138 == -138
  49. Time taken: 3018ms
  50. Time taken: 3231ms
  51. Time taken: 432ms
  52. Time taken: 487ms
  53.  
  54. // Test performed on Windows 7 Ultimate 64-bit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement