Guest User

Untitled

a guest
Apr 16th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. public class Meow {
  2.     static public final float PI = 3.1415927f;
  3.     static public final float PI2 = PI * 2;
  4.  
  5.     static private final int SIN_BITS = 14; // 16KB. Adjust for accuracy.
  6.     static private final int SIN_MASK = ~(-1 << SIN_BITS);
  7.     static private final int SIN_COUNT = SIN_MASK + 1;
  8.  
  9.     static private final float radFull = PI * 2;
  10.     static private final float degFull = 360;
  11.     static private final float radToIndex = SIN_COUNT / radFull;
  12.     static private final float degToIndex = SIN_COUNT / degFull;
  13.  
  14.     static public final float degreesToRadians = PI / 180;
  15.  
  16.     static private class Sin {
  17.         static final float[] table = new float[SIN_COUNT];
  18.  
  19.         static {
  20.             for (int i = 0; i < SIN_COUNT; i++)
  21.                 table[i] = (float)Math.sin((i + 0.5f) / SIN_COUNT * radFull);
  22.             for (int i = 0; i < 360; i += 90) {
  23.                 table[(int)(i * degToIndex) & SIN_MASK] = (float)Math.sin(i * degreesToRadians);
  24.             }
  25.         }
  26.     }
  27.  
  28.     static public void main (String[] args) throws Exception {
  29.         float b = 1;
  30.         while (b != 0) {
  31.             System.gc();
  32.             Thread.sleep(100);
  33.             System.gc();
  34.             Thread.sleep(50);
  35.  
  36.             float a;
  37.             long s, e;
  38.             s = System.nanoTime();
  39.             for (int i = 0; i < 1250; i++)
  40.                 for (a = -MathUtils.PI2; a < MathUtils.PI2; a += 0.0001f)
  41.                     b += a;
  42.             e = System.nanoTime();
  43.             long loopTime = e - s;
  44.  
  45.             System.gc();
  46.             Thread.sleep(100);
  47.             System.gc();
  48.             Thread.sleep(50);
  49.  
  50.             s = System.nanoTime();
  51.             for (int i = 0; i < 1250; i++)
  52.                 for (a = -MathUtils.PI2; a < MathUtils.PI2; a += 0.0001f)
  53.                     b += sin(a);
  54.             e = System.nanoTime();
  55.             System.out.println("MathUtils.sin " + (e - s - loopTime) / 1e6);
  56.  
  57.             System.gc();
  58.             Thread.sleep(100);
  59.             System.gc();
  60.             Thread.sleep(50);
  61.  
  62.             s = System.nanoTime();
  63.             for (int i = 0; i < 1250; i++)
  64.                 for (a = -MathUtils.PI2; a < MathUtils.PI2; a += 0.0001f)
  65.                     b += sinQuadratic(a);
  66.             e = System.nanoTime();
  67.             System.out.println("sinQuadratic  " + (e - s - loopTime) / 1e6);
  68.         }
  69.     }
  70.  
  71.     static public float sinQuadratic (float x) {
  72.         if (x < -3.14159265)
  73.             x += 6.28318531;
  74.         else if (x > 3.14159265) x -= 6.28318531;
  75.         if (x < 0) return 1.27323954f * x + 0.405284735f * x * x;
  76.         return 1.27323954f * x - 0.405284735f * x * x;
  77.     }
  78.  
  79.     static public float sin (float radians) {
  80.         int i = (int)(radians * radToIndex) & SIN_MASK;
  81.         return Sin.table[i];
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment