Guest User

Untitled

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