Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dumptruckman.test;
- public class FloorTest {
- static int iterations = 5000;
- public static void main(String[] args) {
- double x = 204.75764;
- double z = -137.25428;
- System.out.println(floor(x) + " == " + cast(x));
- System.out.println(floor(z) + " == " + cast(z));
- testFloor(x);
- testFloor(z);
- testCast(x);
- testCast(z);
- }
- public static void testFloor(double loc) {
- long startTime = System.nanoTime();
- for (int i = 0; i < iterations; i++) {
- floor(loc);
- }
- long timeTaken = System.nanoTime() - startTime;
- System.out.println("Time taken: " + timeTaken / 1000 + "ms");
- }
- public static void testCast(double loc) {
- long startTime = System.nanoTime();
- for (int i = 0; i < iterations; i++) {
- cast(loc);
- }
- long timeTaken = System.nanoTime() - startTime;
- System.out.println("Time taken: " + timeTaken / 1000 + "ms");
- }
- public static int floor(double loc) {
- return (int) Math.floor(loc);
- }
- public static int cast(double loc) {
- int result = (int) loc;
- return loc < 0 ? result - 1 : result;
- }
- }
- // OUTPUT:
- 204 == 204
- -138 == -138
- Time taken: 3018ms
- Time taken: 3231ms
- Time taken: 432ms
- Time taken: 487ms
- // Test performed on Windows 7 Ultimate 64-bit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement